[PyQt] Restarting embedded interpreter with sip generated module

Phil Thompson phil at riverbankcomputing.com
Thu Jan 7 15:51:41 GMT 2010


On Thu, 7 Jan 2010 13:55:20 -0000, <martyn.gigg at stfc.ac.uk> wrote:
> I have an application with Python embedded that contains a sip generated
> module that allows us to script various parts of the application.
> 
> It would be extremely helpful if we were able to restart the interpreter
> without restarting the application. I've called Py_Finalize() and then
> Py_Initialize again, which works fine. The trouble lies with the
> initialization of the sip generated module, in this case a function
called
> initqti. After calling this after a restart all I see is an empty module,
> i.e. help(qti) gives no definitions at all.
> 
> I have tested this with a small standalone piece of code
> 
> #include <Python.h>
> extern "C"
> {
>   void initword();
> }
> 
> int main()
> {
>   PyImport_AppendInittab((char*)"word",initword);
> 
>   Py_Initialize();
>   PyRun_SimpleString("print 'Initialized python'");
>   PyRun_SimpleString("import sys; sys.path.insert(0, '.')");
>   PyRun_SimpleString("import word; w = word.Word('abc'); print
>   w.reverse()");
>   PyRun_SimpleString("help(word)");
>   Py_Finalize();
> 
>   Py_Initialize();
>   PyRun_SimpleString("print 'Initialized python again!'");
>   PyRun_SimpleString("import sys; sys.path.insert(0, '.')");
>   PyRun_SimpleString("import word; w = word.Word('abc'); print
>   w.reverse()");
>   PyRun_SimpleString("help(word)");
>   Py_Finalize();
> 
> }
> 
> where "word" is the same as the example module given in the sip reference
> documentation. I have also attached the complete example.
> 
> Is there something fundamentally incorrect about trying this or something
> that I don't understand about the workings of sip/python.

The sip module itself, and maybe generated modules, don't do a proper job
when finalising so I'm not sure what would happen if they were
reinitialised.

Phil


More information about the PyQt mailing list