-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
338 lines (273 loc) · 8.75 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
//#include<Python.h>
////#include<boost/python.hpp>
//int main() {
// Py_Initialize();
// PyRun_SimpleString("print(12)");
//
//}
#include<iostream>
#include<string>
#include<vector>
#include <boost/foreach.hpp>
#include <boost/python.hpp>
#include "to_python_converter.h"
#include "from_python_converter.h"
#include "handle_error.h"
#include "container_converters.h"
#include "access.h"
#include "exec_script.h"
#include "interpretator_info.h"
#include <thread>
#include <fstream>
namespace py = boost::python;
//using namespace py;
template<typename T, typename U>
py::ConvertableTypes py::From_python_converter<T, U>::type;
void error_handling_test() {
std::cout << "error_handling_test started" << std::endl;
// 1. std::string parse_python_exception()
//before
try {
py::object main_module = py::import("fake_module");
}
catch (py::error_already_set const &) {
//user must call various Python/C API functions to get information about the exception
PyErr_Print();
//....
//....
}
//now
try {
py::object main_module = py::import("fake_module");
}
catch (py::error_already_set const &) {
//call only parse_python_exception() function to get information about the exception
std::string perror_str = py::parse_python_exception();
std::cout << "Error in Python: " << perror_str << std::endl;
}
// 2. template <class return_type>
// return_type call_python_func(boost::function<return_type()> to_call, const std::string &error_pre);
try {
py::call_python_function<py::object>(std::bind(&py::import, "fake_module"), "failed to import fake_module: ");
}
catch (std::runtime_error& e) {
std::cout << "runtome error " << e.what() << std::endl;
}
std::cout << "error_handling_test ended\n" << std::endl;
}
//some user defined type
class SomeClass {
public:
SomeClass(long i)
: value(i) {}
long get_value() const { return value; }
void set_value(const long v) { value = v; }
private:
long value;
};
long some_class_to_int(const SomeClass& obj)
{
return obj.get_value();
}
py::object to_python_converter_test()
{
std::cout << "to_python_converter_test started" << std::endl;
//user want to convert the user defined class's object into py::object
//before
try {
//to convert user defined c++ type to py::object users must define a struct(class) with a convert function for each newly defined c++ type
struct SomeClass_to_py_int {
static PyObject* convert(const SomeClass & obj)
{
return boost::python::incref(boost::python::object(obj.get_value()).ptr());
}
};
py::to_python_converter<SomeClass, SomeClass_to_py_int>();
SomeClass some_class_obj(0);
py::object obj(some_class_obj);
}
catch (py::error_already_set const &) {
//call only parse_python_exception() function to get information about the exception
std::string perror_str = py::parse_python_exception();
std::cout << "Error in Python: " << perror_str << std::endl;
}
//now
try {
// register the to-python converter
py::to_python_converter<SomeClass, py::To_python_object<SomeClass, long, some_class_to_int>>();
}
catch (py::error_already_set const &) {
//call only parse_python_exception() function to get information about the exception
std::string perror_str = py::parse_python_exception();
std::cout << "Error in Python: " << perror_str << std::endl;
}
/////////////////////////
SomeClass some_class_obj(10);
py::object obj(some_class_obj);
std::cout << "to_python_converter_test ended\n" << std::endl;
return obj;
}
void from_python_converter_test(py::object obj)
{
std::cout << "from_python_converter_test started" << std::endl;
//user want to do the opposite thing of previous example: convert py::object to c++ object
//before
try {
//to convert py::object to C++ object users must define a struct(class) with a convert function for each newly defined c++ type
struct SomeClass_from_python_int
{
SomeClass_from_python_int()
{
boost::python::converter::registry::push_back(
&convertible,
&construct,
boost::python::type_id<SomeClass>());
}
// Determine if obj_ptr can be converted in a SomeClass
static void* convertible(PyObject* obj_ptr)
{
if (!PyLong_Check(obj_ptr)) return 0;
return obj_ptr;
}
// Convert obj_ptr into a SomeClass
static void construct(
PyObject* obj_ptr,
boost::python::converter::rvalue_from_python_stage1_data* data)
{
long value = PyLong_AsLong(obj_ptr);
assert(value);
void* storage = (
(boost::python::converter::rvalue_from_python_storage<SomeClass>*)
data)->storage.bytes;
// in-place construct the new SomeClass
new (storage)SomeClass(value);
data->convertible = storage;
}
};
// register the from-python converter
SomeClass_from_python_int();
}
catch (py::error_already_set const &) {
//call only parse_python_exception() function to get information about the exception
std::string perror_str = py::parse_python_exception();
std::cout << "Error in Python: " << perror_str << std::endl;
}
//now
try {
py::From_python_converter<SomeClass, long>::register_converter(py::ConvertableTypes::LONG);
}
catch (py::error_already_set const &) {
//call only parse_python_exception() function to get information about the exception
std::string perror_str = py::parse_python_exception();
std::cout << "Error in Python: " << perror_str << std::endl;
}
// extruct SomeClass from obj
SomeClass c_object = py::extract<SomeClass>(obj);
std::cout << "checking if the convertion done correctly: obj.value == " << c_object.get_value() << std::endl;
std::cout << "from_python_converter_test ended\n" << std::endl;
}
void container_converters_test()
{
std::cout << "container_converters_test started" << std::endl;
std::vector<int> v{ 1, 3, 2, 5, 4 };
// 1. py::list form std::vector
//before
boost::python::list list;
for (const auto & item : v) {
list.append(item);
}
//now
py::list list_ = py::std_vector_to_python_list(v);
// 2. py::tuple from std::vector
//before
boost::python::list list1;
for (const auto & item : v) {
list1.append(item);
}
py::tuple t(list1);
//now
py::tuple t_ = py::std_vector_to_python_tuple(v);
std::map<int, double> m{ { 1, 0.1 }, { 2, 0.2 }, { 3, 0.3 } };
// 3. py::dict from std::map
//before
boost::python::dict d;
for (const auto& item : m) {
d[item.first] = item.second;
}
//now
boost::python::dict d_ = py::std_map_to_python_dict(m);
//------------------------------------\\
// 4. std::vector from py:: list, tuple ...
//before
v = std::vector<int>(py::stl_input_iterator<int>(t), py::stl_input_iterator<int>());
v = std::vector<int>(py::stl_input_iterator<int>(list), py::stl_input_iterator<int>());
//now
v = py::to_std_vector<int>(t_);
v = py::to_std_vector<int>(list);
// 5. std::map from py::dict
std::map<int, double> m1 = py::python_dict_to_std_map<int, double>(d);
std::cout << "container_converters_test ended\n" << std::endl;
}
void access_test()
{
std::cout << "access_test started" << std::endl;
py::object main_module = py::import("__main__");
py::object main_namespace = main_module.attr("__dict__");
py::object ignored = py::exec_file("hello1.txt", main_namespace);
std::cout << "b == " << py::get_global_variable<int>(main_module, "b") << std::endl;
std::cout << "A::i == " << py::get_member_variable<int>(main_module, "A", "i") << std::endl;
std::cout << "access_test ended\n" << std::endl;
}
void exec_script_test(py::object main_namespace)
{
std::cout << "exec_scrypt_test started" << std::endl;
py::object result;
double t;
std::tie(result, t) = _exec_file("hello1.txt", main_namespace);
std::cout << "execution time = " << t << std::endl;
std::cout << "exec_scrypt_test ended\n" << std::endl;
}
void interpretator_info_test()
{
std::ofstream out("out.txt");
std::streambuf *coutbuf = std::cout.rdbuf(); //save old buf
std::cout.rdbuf(out.rdbuf());
std::cout << "interpretator_info_test started" << std::endl;
py::print_interpreter_info();
std::cout << "interpretator_info_test ended\n" << std::endl;
}
void tf()
{
for (int i = 0; i <= 10; ++i)
{
std::cout << "tf" << i << " ";
}
}
void thread_test() {
std::thread t(tf);
for (int i = 0; i <= 100; ++i)
{
std::cout << "M" << i << " ";
}
t.join();
}
int main() {
thread_test();
try {
Py_Initialize();
py::object main_module = py::import("__main__");
py::object main_namespace = main_module.attr("__dict__");
error_handling_test();
py::object obj = to_python_converter_test();
from_python_converter_test(obj);
container_converters_test();
access_test();
exec_script_test(main_namespace);
interpretator_info_test();
}
catch (py::error_already_set const &) {
//call only parse_python_exception() function to get information about the exception
std::string perror_str = py::parse_python_exception();
std::cout << "Error in Python: " << perror_str << std::endl;
}
}