-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
162 lines (130 loc) · 4.35 KB
/
main.cpp
File metadata and controls
162 lines (130 loc) · 4.35 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
//
#include "PythonInterpreter.h"
void add_cwd_in_python_path();
int initial(int argc, char** argv);
bool callPythonInterpreter(int argc, char** argv);
bool callPythonInterpreterMultipleThreads(void);
int main(int argc, char *argv[])
{
if (argc < 3)
{
printf("Usage: exe_name python_source function_name\n");
return 1;
}
//initial(argc, argv);
/*int i = 0;
while(i++ < 30)
callPythonInterpreter(argc, argv);*/
callPythonInterpreterMultipleThreads();
}
bool callPythonInterpreterMultipleThreads(void)
{
auto py = new Interpret::PythonIntepreter();
auto res = py->loadModule("spam");
std::vector<std::pair<std::string,std::vector<std::string>>>activationRecords;
activationRecords.push_back(std::make_pair<std::string,std::vector<std::string>>("foo",{}));
activationRecords.push_back(std::make_pair<std::string,std::vector<std::string>>("foo",{}));
activationRecords.push_back(std::make_pair<std::string,std::vector<std::string>>("foo",{}));
activationRecords.push_back(std::make_pair<std::string,std::vector<std::string>>("foo",{}));
activationRecords.push_back(std::make_pair<std::string,std::vector<std::string>>("foo",{}));
activationRecords.push_back(std::make_pair<std::string,std::vector<std::string>>("foo",{}));
activationRecords.push_back(std::make_pair<std::string,std::vector<std::string>>("foo",{}));
activationRecords.push_back(std::make_pair<std::string,std::vector<std::string>>("foo",{}));
activationRecords.push_back(std::make_pair<std::string,std::vector<std::string>>("foo",{}));
activationRecords.push_back(std::make_pair<std::string,std::vector<std::string>>("foo",{}));
activationRecords.push_back(std::make_pair<std::string,std::vector<std::string>>("foo",{}));
activationRecords.push_back(std::make_pair<std::string,std::vector<std::string>>("foo",{}));activationRecords.push_back(std::make_pair<std::string,std::vector<std::string>>("foo",{}));
activationRecords.push_back(std::make_pair<std::string,std::vector<std::string>>("foo",{}));
activationRecords.push_back(std::make_pair<std::string,std::vector<std::string>>("foo",{}));
activationRecords.push_back(std::make_pair<std::string,std::vector<std::string>>("foo",{}));
res = py->callFunctions(activationRecords);
assert (res == 0);
delete py;
return true;
}
bool callPythonInterpreter(int argc, char** argv)
{
auto py = new Interpret::PythonIntepreter();
auto res = py->loadModule(argv[1]);
if(res < 0) return false;
res = py->callFunction(argv[2], [&]{
std::vector<std::string> toString;
for(int i = 3; i<argc;i++) toString.push_back(argv[i]);
return toString;
}());
if(res < 0) return false;
res = py->callFunction(argv[2], [&]{
std::vector<std::string> toString;
for(int i = 3; i<argc;i++) toString.push_back(argv[i]);
return toString;
}());
delete py;
return res;
}
void add_cwd_in_python_path()
{
PyObject *sys = PyImport_ImportModule("sys");
PyObject *path = PyObject_GetAttrString(sys, "path");
PyList_Append(path, PyUnicode_FromString("."));
}
int initial(int argc, char** argv)
{
int i;
PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pValue;
// Initialize the Python Interpreter
Py_Initialize();
add_cwd_in_python_path();
// Build the name object
pName = PyString_FromString(argv[1]);
// Load the module object
pModule = PyImport_Import(pName);
assert(pModule);
// pDict is a borrowed reference
pDict = PyModule_GetDict(pModule);
// pFunc is also a borrowed reference
pFunc = PyDict_GetItemString(pDict, argv[2]);
if (PyCallable_Check(pFunc))
{
// Prepare the argument list for the call
if( argc > 3 )
{
pArgs = PyTuple_New(argc - 3);
for (i = 0; i < argc - 3; i++)
{
pValue = PyInt_FromLong(atoi(argv[i + 3]));
if (!pValue)
{
PyErr_Print();
return 1;
}
PyTuple_SetItem(pArgs, i, pValue);
}
pValue = PyObject_CallObject(pFunc, pArgs);
if (pArgs != NULL)
{
Py_DECREF(pArgs);
}
} else
{
pValue = PyObject_CallObject(pFunc, NULL);
}
if (pValue != NULL)
{
printf("Return of call : %d\n", (int)PyInt_AsLong(pValue));
Py_DECREF(pValue);
}
else
{
PyErr_Print();
}
} else
{
PyErr_Print();
}
// Clean up
Py_DECREF(pModule);
Py_DECREF(pName);
// Finish the Python Interpreter
Py_Finalize();
return 0;
}