-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-boost-python.cpp
More file actions
64 lines (43 loc) · 1.64 KB
/
Copy pathtest-boost-python.cpp
File metadata and controls
64 lines (43 loc) · 1.64 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
#include <cassert>
#include <string>
#include <iostream>
#define BOOST_BIND_GLOBAL_PLACEHOLDERS
#include <boost/python.hpp>
#include <boost/python/dict.hpp>
#define PY_SSIZE_T_CLEAN
#include <Python.h>
using namespace std;
int main()
{
namespace bp = boost::python;
//const wchar_t PythonFilename[1024] = L"/home/cpruefer/Repos/data-gateway/python/config.py";
const string PScript = "/home/cpruefer/Repos/data-gateway/python/config.py";
//const wstring PScriptWide = L"/home/cpruefer/Repos/data-gateway/python/config.py";
FILE* fp = fopen(PScript.c_str(), "r");
PyObject *MainModule, *MainDict, *ResultObj, *ResultObjDict;
//Py_SetProgramName(PScriptWide.c_str());
Py_InitializeEx(0);
//Py_Initialize();
MainModule = PyImport_ImportModule("__main__");
MainDict = PyModule_GetDict(MainModule);
PyRun_SimpleFile(fp, PScript.c_str());
//PyRun_SimpleString("config='test'");
ResultObj = PyMapping_GetItemString(MainDict, "config");
string ResultJSON = PyUnicode_AsUTF8(ResultObj);
cout << ResultJSON << endl;
ResultObjDict = PyMapping_GetItemString(MainDict, "config_dict");
/*
object MainModuleBP = import("__main__");
object MainNamespace = MainModuleBP.attr("__dict__");
*/
bp::dict mn_dict = bp::extract<bp::dict>(ResultObjDict);
//object test(handle<>(borrowed(ResultObjDict)));
string result = "";
bp::list items = mn_dict.items();
for (auto i=0; i<len(items); ++i) {
string dict_key = bp::extract<string>(items[i][0]);
cout << "Root dict key:" << dict_key << endl;
result = dict_key;
}
assert(result == "config");
}