-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathembdding.cpp
More file actions
68 lines (61 loc) · 1.63 KB
/
embdding.cpp
File metadata and controls
68 lines (61 loc) · 1.63 KB
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
#include <iostream>
#include <string>
#include <boost/python.hpp>
using namespace boost::python;
void running_python_code(const char *code)
{
object main_module = import("__main__");
object main_namespace = main_module.attr("__dict__");
object result = exec(code, main_namespace);
}
void get_result()
{
int five_squared;
object main_module = import("__main__");
object main_namespace = main_module.attr("__dict__");
// method 1
object ignored = exec("result = 5 ** 2", main_namespace);
five_squared = extract<int>(main_namespace["result"]);
std::cout << "In cpp, method 1, five_squared: " << five_squared << std::endl;
// method 2
object result = eval("5 ** 2");
five_squared = extract<int>(result);
std::cout << "In cpp, method 2, five_squared: " << five_squared << std::endl;
}
void exception_handling()
{
try
{
object result = eval("5/0");
std::cout << "I will never be printed" << std::endl;
}
catch (error_already_set const &)
{
if (PyErr_ExceptionMatches(PyExc_ZeroDivisionError))
{
std::cout << "catch ZeroDivisionError in cpp" << std::endl;
PyErr_Print();
}
else
PyErr_Print();
}
}
void sleep_for_20_seconds()
{
object main_module = import("__main__");
object main_namespace = main_module.attr("__dict__");
exec("print(\"In python, going to sleep for 20 seconds\")\n"
"import time\n"
"time.sleep(20)", main_namespace);
}
int main()
{
Py_Initialize();
running_python_code("import sys\n"
"print(\"I am called from cpp\")\n"
"print(\"sys.version: \", sys.version)");
get_result();
exception_handling();
sleep_for_20_seconds();
return 0;
}