Getting output from embedded python program

K

Kim

Hi everyone,
I'm writing a embeded python program, and I want to evaluate some
expression by calling function:

PyRun_SimpleString("print 'hello'")

I don't want to output it to stdout but putting it into string somehow
sothat I can process it.

Do anybody know how I can do that?
Thanks very much
Kim
 
B

Brad Clements

_
Kim said:
Hi everyone,
I'm writing a embeded python program, and I want to evaluate some
expression by calling function:

PyRun_SimpleString("print 'hello'")

I don't want to output it to stdout but putting it into string somehow
sothat I can process it.

Do anybody know how I can do that?
Thanks very much
Kim


Sure.

You can create a StringIO object and assign it sys.stdout

Or, you can create C "object" with a write method and assign an instance of
that object to sys.stdout.
 
R

Rick L. Ratzel

Kim said:
Hi everyone,
I'm writing a embeded python program, and I want to evaluate some
expression by calling function:

PyRun_SimpleString("print 'hello'")

I don't want to output it to stdout but putting it into string somehow
sothat I can process it.

Here is a way to get the result of a Python expression eval from C
(derived from example at
http://elmer.sourceforge.net/PyCon04/elmer_pycon04.html ) ...obviously,
if you evaluate a print statement, you will still get output on stdout
though:

....
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.
 
R

Roberto

With more try i can run "PyRun_SimpleFile" !

Anyway cant do the same with "PyRun_File" because it will crash my app :

PyRun_File(fpIn,"temp.py",Py_file_input,evalModule ,evalDict);

evalModule = PyImport_AddModule( (char*)"__main__" );
evalDict = PyModule_GetDict( evalModule );
evalVal = PyDict_GetItemString( evalDict, "result" );

This will crash the app :(
Bye
 
R

Rick Ratzel

Thank you! I have never used PyRun_File(), but from the docs it
looks like it would work in a similar fashion (and may allow you to
leave behind a file for debugging later).

Like the presentation suggests, you might want to try Elmer for
automatically generating a "native" C interface for a Python module. I
don't know the details of your project, but in most of my experiences it
is nicer/easier than using the Python/C API directly.

Rick.
 
J

Jeff Epler

PyRun_File expects dicts for both the third and fourth parameters.
The code you wrote appears to use one module object and one dict object,
but in the (incomplete, non-runnable) code you included, you didn't even
initialize evalModule/evalDict until after the PyRun_File call, nor did
you check for failures along the way.

Jeff
 
R

Roberto

Hi Jeff,
Thanks for the reply!
PyRun_File expects dicts for both the third and fourth parameters.

I've looked in python manuals but i've missed out this, thanks for
pointing me in the right direction.

I will try with dicts and post result later.

Thanks Again,
Roberto
 
K

Kim

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
 
R

Rick L. Ratzel

I'm not sure, but I think your problem is related to the globals and
locals args to PyRun_String(). If you're trying to use PyRun_String to
evaluate within the scope of your module, you need to provide the
globals dictionary (as returned by PyEval_GetGlobals() or similar) and
the locals dictionary (the dictionary of the module itself), as well as
the start token (like Py_eval_input as defined in Python.h). Also, if
you're getting NULL back (which means an exception was raised), you
might want to check for the exception value which could give you more
clues as to what went wrong...PyErr_Print() will print the traceback to
stderr.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top