Getting output from embedded python program

Discussion in 'Python' started by Kim, Apr 19, 2004.

  1. Kim

    Kim Guest

    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
    Kim, Apr 19, 2004
    #1
    1. Advertising

  2. _
    "Kim" <> wrote in message
    news:...
    > 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
    Brad Clements, Apr 19, 2004
    #2
    1. Advertising

  3. 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.
    Rick L. Ratzel, Apr 20, 2004
    #3
  4. Kim

    Roberto Guest

    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.
    >
    >
    >
    Roberto, Apr 20, 2004
    #4
  5. Kim

    Roberto Guest

    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.
    >
    >
    >
    Roberto, Apr 20, 2004
    #5
  6. Kim

    Rick Ratzel Guest

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

    >
    >
    >
    Rick Ratzel, Apr 20, 2004
    #6
  7. Kim

    Jeff Epler Guest

    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
    Jeff Epler, Apr 20, 2004
    #7
  8. Kim

    Roberto Guest

    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:<>...
    > 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
    Roberto, Apr 21, 2004
    #8
  9. Kim

    Kim Guest

    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.
    Kim, Apr 22, 2004
    #9
  10. 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.
    Rick L. Ratzel, May 19, 2004
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. temp1111

    embedded python - mpatrol output

    temp1111, May 19, 2004, in forum: Python
    Replies:
    1
    Views:
    250
    Stefan Seefeld
    May 19, 2004
  2. Colin Paul Gloster
    Replies:
    48
    Views:
    1,879
    Colin Paul Gloster
    Apr 10, 2007
  3. Thomas Dodds

    Databind an embedded control in an embedded datagrid

    Thomas Dodds, Jul 26, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    397
    Thomas Dodds
    Jul 26, 2004
  4. Trans
    Replies:
    11
    Views:
    297
    micathom
    Sep 5, 2007
  5. Num GG
    Replies:
    2
    Views:
    351
    Num GG
    Nov 17, 2008
Loading...

Share This Page