By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,996 Members | 1,498 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,996 IT Pros & Developers. It's quick & easy.

several interpreters and suspend script

P: n/a
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
{
public:
virtual ~cPython();
cPython();
public:
int Run( const char* iArgs );

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

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

mLocalDictionary = PyDict_Copy( PyModule_GetDict( mMainModule ) );
}
int
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
"MyModule"
InitPythonWindow( pyt.Module() ); //add the "class" window in the
module "MyModule"
pyt.Dictionary( L"MyModule ); //create the 2 dictionary

pyt.Run( L"MyPath/MyFile.py" );
//---------------------------------------------------------------------------------------------------------------------

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

Regards
Oct 24 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.