-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.h
More file actions
176 lines (143 loc) · 5.2 KB
/
Copy pathdemo.h
File metadata and controls
176 lines (143 loc) · 5.2 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
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
#include <Python.h>
#include <assert.h>
#include <iostream>
#include <vector>
#include <string>
#include <functional>
#include <map>
namespace Interpret{
enum InterpreterStatus{
OK = 0,
FUNCTION_NOT_FOUND = -1,
MODULE_NOT_FOUND = -2,
RETURNED_NULL = -3,
NOT_CALLABLE_OBJECT = -4,
UNKNOWN_ERROR = -5,
ARGUMENT_ERROR = -6,
UNKNOWN_INTEGER_ERROR = -7
};
class PythonInterpreter
{
private:
std::vector<PyObject*> m_Globals;
PyObject * m_CurrentModule = nullptr;
PyObject * m_CurrentTable = nullptr;
PyObject * m_CurrentModuleName = nullptr;
bool m_Initialized = false;
std::map<std::string, std::pair<PyObject*, PyObject*>> m_LoadedModules;
public:
PythonInterpreter()
{
}
~PythonInterpreter()
{
cleanUp();
// Finish the Python Interpreter
if(m_Initialized) {
//std::cout << "PythonInterpreter destroyed!" << std::endl;
Py_Finalize();
}
}
bool isInitialized(){return m_Initialized;}
InterpreterStatus loadModule(const std::string& module)
{
if(!m_Initialized) {
Py_Initialize();
addCwdInPythonPath();
m_Initialized = true;
//std::cout << "PythonInterpreter initialized!" << std::endl;
}
//If the module isnt here yet, or the same module has being loaded earlier, load it
if(m_LoadedModules.find(module.c_str()) == m_LoadedModules.end() ) {
//std::cout << "Module init" << std::endl;
m_CurrentModuleName = PyString_FromString(module.c_str());
// Load the module object
m_CurrentModule = PyImport_Import(m_CurrentModuleName);
if(!m_CurrentModule) return MODULE_NOT_FOUND;
// m_CurrentTable is a borrowed reference
m_CurrentTable = PyModule_GetDict(m_CurrentModule);
m_LoadedModules[module] = std::make_pair(m_CurrentModule,m_CurrentModuleName);
}
else {
m_CurrentModuleName = m_LoadedModules[module.c_str()].second;
m_CurrentModule = m_LoadedModules[module.c_str()].first;
m_CurrentTable = PyModule_GetDict(m_CurrentModule);
}
return OK;
}
PyObject* setFunctionArgs(const std::vector<std::string>& args)
{
int argsN = (int)args.size();
auto pArgs = PyTuple_New(argsN );
PyObject *pValue = nullptr;
for (auto i = 0; i < argsN ; i++) {
//std::cout << args[i + 0] << std::endl;
pValue = PyInt_FromLong(atoi(args[i].c_str()));
if (!pValue) {
PyErr_Print();
return nullptr;//UNKNOWN_INTEGER_ERROR;
}
PyTuple_SetItem(pArgs, i, pValue);
}
return pArgs;
}
InterpreterStatus callFunction(const std::string& function, const std::vector<std::string> args)
{
PyObject* currentFunction = PyDict_GetItemString(m_CurrentTable, function.c_str());
if(!currentFunction) {std::cout<<"function not found"<<std::endl;return FUNCTION_NOT_FOUND;}
if (PyCallable_Check(currentFunction)) {
PyObject *pValue = nullptr;
// Prepare the argument list for the call
int argsN = (int)args.size();
if( argsN > 0 ) {
auto pArgs = setFunctionArgs(args);
if(!pArgs) return UNKNOWN_INTEGER_ERROR;
pValue = PyObject_CallObject(currentFunction, pArgs);
if (pArgs != nullptr) {
Py_DECREF(pArgs);
}
}
else {
pValue = PyObject_CallObject(currentFunction, nullptr);
}
if (pValue != nullptr) {
printf("Return of call : %d\n", (int)PyInt_AsLong(pValue));
Py_DECREF(pValue);
}
else {
PyErr_Print();
return ARGUMENT_ERROR;
}
}
else {
//std::cout << "Cant call it" << std::endl;
PyErr_Print();
return NOT_CALLABLE_OBJECT;
}
return OK;
}
private:
void addCwdInPythonPath()
{
PyObject *sys = PyImport_ImportModule("sys");
PyObject *path = PyObject_GetAttrString(sys, "path");
PyList_Append(path, PyUnicode_FromString("."));
m_Globals.push_back(sys);
m_Globals.push_back(path);
}
void cleanUp()
{
// Clean up
//std::cout << "Destroying " << m_Locals.size() << " locals" << std::endl;
for(auto var : m_LoadedModules) {
Py_DECREF((var).second.first);
Py_DECREF((var).second.second);
}
m_LoadedModules.clear();
//std::cout << "Destroying " << m_Globals.size() << " globals" << std::endl;
for(auto &var : m_Globals) {
Py_DECREF(var);
}
}
};
};