-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
108 lines (89 loc) · 3.26 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
#include <Python.h>
#include <boost/python.hpp>
#include <string>
#include <iostream>
#include <cstdlib>
namespace bp = boost::python;
void boost_exec_file()
{
std::string py_filename {"sample_python_code.py"};
std::string py_func_name {"get_number"};
try {
bp::object module = bp::import("__main__");
bp::object name_space = module.attr("__dict__");
bp::exec_file(py_filename.c_str(), name_space, name_space);
bp::object py_func = name_space[py_func_name.c_str()];
bp::object py_res = py_func();
int res = bp::extract<int>(py_res);
std::cout << "the magic number is: [" << res << "]" << std::endl;
}
catch(bp::error_already_set& ex) {
std::cerr << "ERROR: python exception" << std::endl;
PyErr_Print();
}
}
void boost_exec()
{
std::string py_filename {"sample_python_code.py"};
std::string py_func_name {"get_number"};
try {
bp::object module = bp::import("__main__");
bp::object name_space = module.attr("__dict__");
bp::exec("print('hello from python in c++')", name_space, name_space);
}
catch(bp::error_already_set& ex) {
std::cerr << "ERROR: python exception" << std::endl;
PyErr_Print();
}
}
void boost_exec_run_py_func()
{
std::string py_filename {"sample_python_code.py"};
std::string py_func_name {"get_number"};
try {
bp::object module = bp::import("__main__");
bp::object name_space = module.attr("__dict__");
bp::exec("from sample_python_code import get_number", name_space, name_space);
bp::exec("get_number", name_space, name_space);
}
catch(bp::error_already_set& ex) {
std::cerr << "ERROR: python exception" << std::endl;
PyErr_Print();
}
}
void boost_import_custom_module()
{
std::string py_module_name {"sample_python_code"};
std::string py_func_name {"get_number"};
try {
// >>> import the module from disk
bp::object module = bp::import(py_module_name.c_str());
auto numb = module.attr(py_func_name.c_str())();
int res = bp::extract<int>(numb);
std::cout << "the magic number is: [" << res << "]" << std::endl;
}
catch(bp::error_already_set& ex) {
std::cerr << "ERROR: python exception" << std::endl;
PyErr_Print();
}
}
// A simle example with boost::python::exec():
// https://stackoverflow.com/questions/50154064/call-a-python-function-from-c-using-boost-python
// A nice example of boost python:
// https://stackoverflow.com/questions/38620134/how-to-import-a-function-from-python-file-by-boost-python
// Add a dir to python sys.path so a python module can be found:
// https://stackoverflow.com/questions/46669766/boost-python-how-to-import-py-in-imported-python-file
int main()
{
// Allow Python to load modules from the current directory.
setenv("PYTHONPATH", ".", 1); // must be called before Py_Initialize().
Py_Initialize();
std::cout << ">> boost_exec_file():" << std::endl;
boost_exec_file();
std::cout << ">> boost_exec():" << std::endl;
boost_exec();
std::cout << ">> boost_exec_run_py_func():" << std::endl;
boost_exec_run_py_func();
std::cout << ">> boost_import_custom_module():" << std::endl;
boost_import_custom_module();
}