dongyukun
2025-02-21 2bf209dce77d2a8236dbeb14e110e300acc45452
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
#include"pch.h"
#include"pyutils.h"
 
 
PyObject* create_py_module(string module_name) {
    Py_Initialize();
    PyRun_SimpleString("import sys");
    PyRun_SimpleString("import os");
    PyRun_SimpleString("sdk_path = os.getenv('MDK_PKGS')");
    string pyd_home = "sys.path.append(sdk_path)";
    PyRun_SimpleString(pyd_home.c_str());
 
    PyObject* pModule = PyImport_ImportModule(module_name.c_str());
    return pModule;
}
 
int py_initialize() {
    Py_Initialize();
    if (!Py_IsInitialized()) {
        return 0;
    }
    else {
        PyRun_SimpleString("import sys");
        PyRun_SimpleString("import os");
        PyRun_SimpleString("sdk_path = os.getenv('MDK_PKGS')");
        string pyd_home = "sys.path.append(sdk_path)";
        PyRun_SimpleString(pyd_home.c_str());
        //char* sdk_path = getenv("MDK_PKGS");
 
        PyEval_InitThreads();
        int nInit = PyEval_ThreadsInitialized();
        if (nInit) {
            PyEval_ReleaseThread(PyThreadState_Get());
        }
        else {
            return 0;
        }
    }
}
 
int py_finalize() {
    int nint = PyGILState_Check();
    PyGILState_STATE gstate;
    if (!nint) {
        gstate = PyGILState_Ensure();
    }
    Py_Finalize();
    return nint;
    //PyGILThreadLock lock;
}
 
int py_isInitialized() {
    return Py_IsInitialized();
}