-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembedded-python.c
More file actions
45 lines (38 loc) · 1.14 KB
/
embedded-python.c
File metadata and controls
45 lines (38 loc) · 1.14 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
#include <Python.h>
int main(int argc, char *argv[])
{
PyObject *pName, *pModule, *pDict, *pFunc;
if (argc < 3) {
printf("Usage: exe_name python_source func_name\n");
return 1;
}
// Initialize python interpreter
printf("=======");
exit;
Py_Initialize();
if (!Py_IsInitialized()) {
printf("Initialize python failed...");
} else {
printf("Initialize successfully...");
}
pName = PyString_FromString(argv[1]);
pModule = PyImport_Import(pName);
if (pModule) {
printf("Load module successfully...");
} else {
printf("Load module failed..");
exit;
}
pDict = PyModule_GetDict(pModule);
pFunc = PyDict_GetItemString(pDict, argv[2]);
if (PyCallable_Check(pFunc)) {
PyObject_CallObject(pFunc, NULL);
} else {
PyErr_Print();
}
// Clean up
Py_DECREF(pModule);
Py_DECREF(pName);
// Finish python interpreter
Py_Finalize();
}