Hi,
I'm trying to embed python into an chat bot I've made in c++. After
googling around and reading docs for almost a day I have a few
questions. First of all it seems like the best way to be able to run
separate scripts in different, isolated environments is to create a
sub-interpreter (by using Py_NewInterpret e()) with an associated thread
for each script/file. Or is this wrong?
The big problem I have is when I want to call a python function. When
loading a script I use the following code:
PyEval_AcquireL ock(); // get global lock so we can create a new interpreter
threadState = Py_NewInterpret er(); // create a new sub-interpreter and
get a pointer to the first thread
PyThreadState_S wap(threadState ); // switch to our new thread state
appModule = PyImport_AddMod ule("hostapp"); // create a module for this
program
PyModule_AddInt Constant(appMod ule, "SEVENTEEN" , 17); // set a test constant
FILE* fp = fopen(filename, "r"); // load the python script file
PyRun_SimpleFil e(fp, filename);
fclose(fp);
PyEval_ReleaseT hread(threadSta te); // set current thread state to NULL
and release global lock
When loading a script that contains:
import hostapp
print "test: ",hostapp.SEVEN TEEN
it prints out "test: 17" nicely after the call to PyRun_SimpleFil e(...).
So far so good, but the problem starts when I try to call a python
function from c++. Here's the code that isn't working:
PyEval_AcquireT hread(threadSta te);
PyObject* func = PyDict_GetItemS tring(PyImport_ GetModuleDict() ,
"testfunc") ;
if(PyCallable_C heck(func)) {
// never get this far
}
PyEval_ReleaseT hread(threadSta te);
PyDict_GetItemS tring always return NULL for me, although I've defined
the function as:
def testfunc():
print "hi there"
in the python file. Running PyRun_SimpleStr ing("testfunc() \n") just
after PyEval_AcquireT hread() works like a charm though. Any ideas? I
kinda get the feeling that I don't get the dict from
PyImport_GetMod uleDict() that I'm expecting.
Thanks in advance..
/Stefan
I'm trying to embed python into an chat bot I've made in c++. After
googling around and reading docs for almost a day I have a few
questions. First of all it seems like the best way to be able to run
separate scripts in different, isolated environments is to create a
sub-interpreter (by using Py_NewInterpret e()) with an associated thread
for each script/file. Or is this wrong?
The big problem I have is when I want to call a python function. When
loading a script I use the following code:
PyEval_AcquireL ock(); // get global lock so we can create a new interpreter
threadState = Py_NewInterpret er(); // create a new sub-interpreter and
get a pointer to the first thread
PyThreadState_S wap(threadState ); // switch to our new thread state
appModule = PyImport_AddMod ule("hostapp"); // create a module for this
program
PyModule_AddInt Constant(appMod ule, "SEVENTEEN" , 17); // set a test constant
FILE* fp = fopen(filename, "r"); // load the python script file
PyRun_SimpleFil e(fp, filename);
fclose(fp);
PyEval_ReleaseT hread(threadSta te); // set current thread state to NULL
and release global lock
When loading a script that contains:
import hostapp
print "test: ",hostapp.SEVEN TEEN
it prints out "test: 17" nicely after the call to PyRun_SimpleFil e(...).
So far so good, but the problem starts when I try to call a python
function from c++. Here's the code that isn't working:
PyEval_AcquireT hread(threadSta te);
PyObject* func = PyDict_GetItemS tring(PyImport_ GetModuleDict() ,
"testfunc") ;
if(PyCallable_C heck(func)) {
// never get this far
}
PyEval_ReleaseT hread(threadSta te);
PyDict_GetItemS tring always return NULL for me, although I've defined
the function as:
def testfunc():
print "hi there"
in the python file. Running PyRun_SimpleStr ing("testfunc() \n") just
after PyEval_AcquireT hread() works like a charm though. Any ideas? I
kinda get the feeling that I don't get the dict from
PyImport_GetMod uleDict() that I'm expecting.
Thanks in advance..
/Stefan