PyRun_String

Cedric Adjih adjih at crepuscule.com
Fri May 19 11:58:53 EDT 2000


Mike Morasky <mikem at wetafx.co.nz> wrote:
> Embedded in a simple c++ program, I've defined a global dictionary and then:
>
>         PyObject *resultObj = PyRun_String(script, Py_file_input, globalDict,
> globalDict);
>         if( !resultObj )
>          {
>                 PyErr_Print();
>            }
>             else
>             {
>                 PyObject_Print(resultObj, stdout, 1);
>                 Py_DECREF(resultObj);
>             }
>
> Anyway, it appears to execute just fine and the errors all print exactly what
> you'd expect but the non-error rusults always print "None". I suspect the int
> arg to PyObject_Print is wrong. I found reference to Py_Print_RAW, which I
> tried but is undefined as a constant.
>
> Anyone have any pointers?

  This looks ok. "None" is the default value (in C the object Py_None).
(Ponder this in a normal Python interpreter: "print [1,2,3].reverse()").

Now, maybe you want to get the final values of a long list of statements.
I don't know the solution:
- Putting a "return" statement should fail ("'return' outside function").
- "Py_eval_input" instead of Py_file_input would return a value, but
  works only for expressions (not statements, or list of statements...)
- "Py_single_input" is a possibility, but does it work?
- a workaround is, for instance:
  . to set "returnValue=..." at the end of the Python code
  . to retrieve the variable "returnValue" in the locals
  dictionnary (which happens to be also 'globalDict' in the code you
  posted) after the "PyRun_String".

-- Cedric





More information about the Python-list mailing list