Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Getting output from embedded python program

Reply
Thread Tools

Getting output from embedded python program

 
 
Kim
Guest
Posts: n/a
 
      04-19-2004
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
 
Reply With Quote
 
 
 
 
Brad Clements
Guest
Posts: n/a
 
      04-19-2004
_
"Kim" <> wrote in message
news: 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




 
Reply With Quote
 
 
 
 
Rick L. Ratzel
Guest
Posts: n/a
 
      04-20-2004
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.



 
Reply With Quote
 
Roberto
Guest
Posts: n/a
 
      04-20-2004
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" <> ha scritto nel messaggio
news:...
> 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.
>
>
>



 
Reply With Quote
 
Roberto
Guest
Posts: n/a
 
      04-20-2004
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" <> ha scritto nel messaggio
news:...
> 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.
>
>
>



 
Reply With Quote
 
Rick Ratzel
Guest
Posts: n/a
 
      04-20-2004
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" <> ha scritto nel messaggio
> news:...
>
>>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.
>>
>>
>>

>
>
>

 
Reply With Quote
 
Jeff Epler
Guest
Posts: n/a
 
      04-20-2004
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

 
Reply With Quote
 
Roberto
Guest
Posts: n/a
 
      04-21-2004
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 <> wrote in message news:<mailman.810.1082487073.20120.python->...
> 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

 
Reply With Quote
 
Kim
Guest
Posts: n/a
 
      04-22-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.

 
Reply With Quote
 
Rick L. Ratzel
Guest
Posts: n/a
 
      05-19-2004

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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Embedded vs. Non-embedded Tests Trans Ruby 11 09-05-2007 11:22 AM
Embedded languages based on early Ada (from "Re: Preferred OS, processor family for running embedded Ada?") Colin Paul Gloster VHDL 48 04-10-2007 10:31 AM
How to display images embedded in e-mail as embedded, not attachments Jim Firefox 4 12-11-2004 05:36 AM
Databind an embedded control in an embedded datagrid Thomas Dodds ASP .Net Datagrid Control 0 07-26-2004 08:20 PM
embedded python - mpatrol output temp1111 Python 1 05-19-2004 02:54 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57