-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonembedded.hpp
More file actions
45 lines (36 loc) · 838 Bytes
/
pythonembedded.hpp
File metadata and controls
45 lines (36 loc) · 838 Bytes
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
#ifndef PYTHONEMBEDDED_HPP
#define PYTHONEMBEDDED_HPP
#include <vector>
#include <string>
#include <boost/python.hpp>
namespace python_embed {
namespace py = boost::python;
py::dict main_namespace;
void init(const std::string& module, void(*initfunc)())
{
try
{
PyImport_AppendInittab(module.c_str(),initfunc);
Py_Initialize();
py::object main_module = py::import("__main__");
main_namespace = py::extract<py::dict>(main_module.attr("__dict__"));
py::import(module.c_str());
}
catch(py::error_already_set& e)
{
PyErr_PrintEx(0);
}
}
void run(std::string script, py::dict namespace_)
{
try
{
py::exec(script.c_str(),namespace_);
}
catch(py::error_already_set& e)
{
PyErr_PrintEx(0);
}
}
}
#endif // PYTHONEMBEDDED_HPP