Getting output from embedded python program
Rick Ratzel
rick.ratzel at magma-da.com
Tue Apr 20 12:30:29 EDT 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" <rick.ratzel at magma-da.com> ha scritto nel messaggio
> news:4084B179.6020807 at 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.
>>
>>
>>
>
>
>
More information about the Python-list
mailing list