Getting output from embedded python program
Kim
thefirstofnovember at yahoo.com
Thu Apr 22 14:50:32 EDT 2004
Hi Rich,
You post is extremely useful. However when I tried to run the
expression in My Module's context, instead of __main__ module. It
always retunns NULL. How can I do that? I tried PyRun_String().. but
didn't work. i don't really understand global and local arguments in
PyRun_String(), token is set to 0?
THanks very much !
Kim
> ...
> PyObject* evalModule;
> PyObject* evalDict;
> PyObject* evalVal;
> char* retString;
>
> PyRun_SimpleString( "result = 'foo' + 'bar'" )
>
> evalModule = PyImport_AddModule( (char*)"__main__" );
> evalDict = PyModule_GetDict( evalModule );
> evalVal = PyDict_GetItemString( evalDict, "result" );
>
> if( evalVal == NULL ) {
> PyErr_Print();
> exit( 1 );
>
> } else {
> /*
> * PyString_AsString returns char* repr of PyObject, which should
> * not be modified in any way...this should probably be copied for
> * safety
> */
> retString = PyString_AsString( evalVal );
> }
> ...
>
> In this case, you need to know that the expression will evaluate to
> a string result in order to call PyString_AsString(). If you don't know
> this, you will have to check the type of the PyObject first.
More information about the Python-list
mailing list