several interpreters and suspend script

Mike mschreiner at
Mon Oct 24 11:10:36 CEST 2005

Hello All,

I'm working ( and a beginner ) with mixing Python scripting and C++.

And I need some little help.  :)
I've searched on the net, but found no answer. So I ask you directly.

Globally, I'd like to do 2 things.

The first, when I'm in the program, I call a script, which call a function 
to open a window.
And when the window is opened the script should stop / wait, until the 
window closes, and then continue.
Does any function exists to do this ?

    import MyModule

    def ok_cb():
        global w
        MyModule.Warning_All( "closing window" )
        w.Close() #after here, the script should continue

    #create the window
    w = MyModule.cPythonWindow( "win", U"win", 0 )

    #add some buttons, and when we push one, the function ok_cb is called
    w.AddActionButton( "ok", "OK", ok_cb )
    w.AddIntegerButton( "int", "Nb", 2541, ok_cb )

    #open the window
    w.Open() #we should stay here until the callback ends (when we push a 
button) and the window close

    #it should show this message when the window closes and not directly 
after the window is created
    MyModule.Warning_All( "exiting script" )

The second problem ( which is in relation with the first ) is when we have 2 
non-modal windows, at the same time.
If the 2 scripts stops/are suspended, all the variables in the scripts must 
be totally independent.
So, I'd like that the 2 "spaces" of these scripts are independent.
I wanted to use the Py_NewInterpreter() but it failed in 
PyThreadState_Swap() with this error : "PyFatalError("Invalid thread state 
for this thread");".
So I use 2 dictionaries for each script, but is it sufficient?
What about the call stack ? Where/how is it saved ?

    class cPython
        virtual ~cPython();
        int Run( const char* iArgs );

        void Module( const char* iModuleName, PyMethodDef* iModuleMethod );
        void Dictionary( const char* iModuleName );

        PyObject*  mMainModule;
        PyObject*  mGlobalDictionary;
        PyObject*  mLocalDictionary;
    cPython::Module( const char* iModuleName, PyMethodDef* iModuleMethod )
        mMainModule = Py_InitModule( iModuleName, iModuleMethod );
        PyDict_Merge( PyModule_GetDict( mMainModule ), PyModule_GetDict( 
PyImport_AddModule( "__main__" ) ), true );
    cPython::Dictionary( const char* iModuleName )
        mGlobalDictionary = PyDict_Copy( PyModule_GetDict( mMainModule ) );
        PyDict_SetItemString( mGlobalDictionary, iModuleName, mMainModule );

        mLocalDictionary = PyDict_Copy( PyModule_GetDict( mMainModule ) );
    cPython::Run( const char* iArgs )
        PyObject* run_string = PyRun_String( iArgs, Py_file_input, 
mGlobalDictionary, mLocalDictionary );
        Py_XDECREF( run_string );
        return 0;

    cPython pyt;
    pyt.Module( L"MyModule", sgMethods ); //create the module "MyModule"
    InitPythonProject( pyt.Module() );  //add the "class" project in the 
module "MyModule"
    InitPythonLayer( pyt.Module() );  //add the "class" layer in the module 
    InitPythonWindow( pyt.Module() );  //add the "class" window in the 
module "MyModule"
    pyt.Dictionary( L"MyModule );   //create the 2 dictionary

    pyt.Run( L"MyPath/" );

If I create more python objects, the dictionaries are different but not the 
module( there is no copy of the module for the next python object ), right?
And can this make problems for globals variables or callstack for each 
suspended script ?

Here, that's all  :)

So, if you have any suggestions ( or even solutions ).

Thanks for reading


More information about the Python-list mailing list