Python c-api and reusing python-objects: works only once

Gabriel Genellina gagsl-py at
Wed Nov 8 22:42:37 CET 2006

At Wednesday 8/11/2006 17:04, karye2004 at wrote:

>I'm trying to access python objects from c++. It works once. Second
>time it hangs.
>Does someone have any clue or some example code or pointer?

In general, try to make a *small* example that reproduces the problem you have.

>And the c++ class executed inside a thread:
>QStringList Pythonizer::getPackages( char *pyModule )
>         // Initialize the Python Interpreter
>         Py_Initialize();
>  [... some code ...]
>         DEBUG_LINE_INFO;
>         if ( PyErr_Occurred() ) {
>                 PyErr_Print();
>                 PyErr_Clear();
>                 return QStringList::QStringList();
>         }
>  [... more code ...]
>         // Finish the Python Interpreter
>         Py_Finalize();
>         return packageList;

You are initializing the interpreter on *each* call to getPackages - 
don't do that. Worse, "inside a thread". Just initialize the 
interpreter when your program begins, and finalize it when your program ends.
Your current code does not call Py_Finalize when an error occurs. The 
next time, you invoke Py_Initialize again; and you shouldn't.

Gabriel Genellina
Softlab SRL 

Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis! 
¡Abrí tu cuenta ya! -

More information about the Python-list mailing list