dengzedong
2024-09-25 f7932f5848903427a2c08f8eb40bc0022410de42
提交 | 用户 | 时间
f7932f 1 #include"pch.h"
D 2 #include"pyutils.h"
3
4
5 PyObject* create_py_module(string module_name) {
6     Py_Initialize();
7     PyRun_SimpleString("import sys");
8     PyRun_SimpleString("import os");
9     PyRun_SimpleString("sdk_path = os.getenv('MDK_PKGS')");
10     string pyd_home = "sys.path.append(sdk_path)";
11     PyRun_SimpleString(pyd_home.c_str());
12
13     PyObject* pModule = PyImport_ImportModule(module_name.c_str());
14     return pModule;
15 }
16
17 int py_initialize() {
18     Py_Initialize();
19     if (!Py_IsInitialized()) {
20         return 0;
21     }
22     else {
23         PyRun_SimpleString("import sys");
24         PyRun_SimpleString("import os");
25         PyRun_SimpleString("sdk_path = os.getenv('MDK_PKGS')");
26         string pyd_home = "sys.path.append(sdk_path)";
27         PyRun_SimpleString(pyd_home.c_str());
28         //char* sdk_path = getenv("MDK_PKGS");
29
30         PyEval_InitThreads();
31         int nInit = PyEval_ThreadsInitialized();
32         if (nInit) {
33             PyEval_ReleaseThread(PyThreadState_Get());
34         }
35         else {
36             return 0;
37         }
38     }
39 }
40
41 int py_finalize() {
42     int nint = PyGILState_Check();
43     PyGILState_STATE gstate;
44     if (!nint) {
45         gstate = PyGILState_Ensure();
46     }
47     Py_Finalize();
48     return nint;
49     //PyGILThreadLock lock;
50 }
51
52 int py_isInitialized() {
53     return Py_IsInitialized();
54 }