473,387 Members | 3,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Getting output from embedded python program

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
9 7585
_
"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
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
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
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
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
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
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
2
by: Rick Olson | last post by:
I'm trying to add a Tkinter interface to an existing C program with embedded python, but seem to have trouble importing Tkinter (or accessing it). I tried a simple program that would run the...
12
by: Brandon | last post by:
Java seems to have taken off as the platform and language of choice for many embedded devices. Would it be feasible for Python(perhaps running on an embedded version of Linux) to act in such a...
8
by: Hugh Macdonald | last post by:
I'm calling a command from within a python script and I need to be able to both catch the output (stdout and stderr) from it and also have the PID (so that I can kill it) I can do one or other...
16
by: Chris Maloof | last post by:
Hello, Does anyone know how I can read the ASCII text from a console window (from another application) in WinXP? It doesn't sound like a major operation, but although I can find the window via...
6
by: Christian Convey | last post by:
Hello, I've got a program that (ideally) perpetually monitors sys.stdin for lines of text. As soon as a line comes in, my program takes some action. The problem is, it seems like a very large...
3
by: ycollet | last post by:
Hello, I've written a C embedded application. I want to open a python gui application in my C program but when I do : PyRun_String( "import gui.py", file_input, pDictionary, pDictionary ); ...
25
by: Eric | last post by:
Hello, after reading some of the book Programming Python it seems that python is something I would like to delve deeper into. The only thing is, I have no idea what I should try and write. So I was...
3
by: Sami Vaisanen | last post by:
Hello group, I'm writing a C++ based application that embeds the python engine. Now I have a problem regarding exception/error information. Is there a way to get the exception message and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.