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

Getting output from embedded python program

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
_
"Kim" <th****************@yahoo.com> wrote in message
news:2a**************************@posting.google.c om...
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.

--
Novell DeveloperNet Sysop #5


Jul 18 '05 #2

P: n/a
Kim wrote:
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.

Jul 18 '05 #3

P: n/a
Hi there,
http://elmer.sourceforge.net/PyCon04/elmer_pycon04.html ) ...obviously,
if you evaluate a print statement, you will still get output on stdout
This tutorial is quite good!

The same thing can be done even with the py_runfile ??
I'm triyng to do such things with no result!

Bye,
Roberto
"Rick L. Ratzel" <ri*********@magma-da.com> ha scritto nel messaggio
news:40**************@magma-da.com... Kim wrote:
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.

Jul 18 '05 #4

P: n/a
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
--
Roberto

"Rick L. Ratzel" <ri*********@magma-da.com> ha scritto nel messaggio
news:40**************@magma-da.com...
Kim wrote:
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.

Jul 18 '05 #5

P: n/a
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.
Roberto wrote:
Hi there,

http://elmer.sourceforge.net/PyCon04/elmer_pycon04.html ) ...obviously,
if you evaluate a print statement, you will still get output on stdout

This tutorial is quite good!

The same thing can be done even with the py_runfile ??
I'm triyng to do such things with no result!

Bye,
Roberto
"Rick L. Ratzel" <ri*********@magma-da.com> ha scritto nel messaggio
news:40**************@magma-da.com...
Kim wrote:
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.


Jul 18 '05 #6

P: n/a
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

Jul 18 '05 #7

P: n/a
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
Jeff Epler <je****@unpythonic.net> wrote in message news:<ma**************************************@pyt hon.org>... 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

Jul 18 '05 #8

P: n/a
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
...
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.

Jul 18 '05 #9

P: n/a

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.

Kim wrote:
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.

Jul 18 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.