Segregating Interpreter Sessions.

Michael Saunders michael at amtec.com
Wed Mar 27 12:53:30 EST 2002


I have a C application embedding the python interpreter in which I would like to
execute python commands. I would like the set of commands to be executed in it's
own interpreter. My use of the code looks something like this (error handling
left out):


/* initialize python somewhere at the beginning of my C application */
Py_Initialize();

...

/*
 * somewhere in the middle of my C application
 * execute some python code in it's own interpreter
 */
PyThreadState *Interpreter = Py_NewInterpreter();
PyRun_SimpleFile(PythonFile, PythonFileName);
Py_EndInterpreter(Interpreter);

...

/* cleanup python somewhere at the end of my C application */

Py_Finalized();



My application is not using threads but after reading the documentation it
appeared that using Py_NewInterpreter() was the correct way to create a "nearly"
private area to run python code. When I get to the Py_Finalized() code I get the
following fatal error from Python:

  Fatal Python error: PyThreadState_Get: no current thread
  Abort(coredump)


Why am I getting this? Do I need to do more thread initialization even though I
am not using threads? Should I be using some other function to create a separate
interpreter environment? If so how do I tell PyRun_SimpleFile to execute in that
environment.

Thanks,
Michael




More information about the Python-list mailing list