Mailing List Archive

Embedding python question: PyRun_String only returns None not what I calculate
What I want to do is call python to run python code and return the results
to
my app. But I cannot find anyway to do this.

It would seem that I should be using PyRun_String to run a piece of python
and return a result object to me. But I can only get a return value of None.
NULL is returned if the code does not run.

How do I get python to return 2 to me when I ask it what 1+1 is?

Below is a fragment of code that I'm using to investigate PyRun_String.

BArry

//
// Test getting an object back from a command
//
char *command = "1+1";

PyObject *m = PyImport_AddModule("__main__");
if (m == NULL)
return EXIT_FAILURE;

PyObject *d = PyModule_GetDict(m);
PyObject *v = PyRun_String(command, Py_file_input, d, d);
if (v == NULL)
{
PyErr_Print();
return EXIT_FAILURE;
}

PyObject *result = PyObject_Str( v );

char *string = PyString_AsString( result );
printf("Python returned: %s", string );

Py_DECREF(result);
Py_DECREF(v);
Embedding python question: PyRun_String only returns None not what I calculate [ In reply to ]
Hopefully someone with more time can give a better answer...

But the short story is that PyRun_String works with statements, not
expressions. You can use it to, eg, import modules, and even call
functions, but you cant get the result.

To see the difference, try from a Python prompt:
>>> cmd = "1+1"
>>> exec cmd
>>> eval(cmd)

You will notice that the functionality you are after is by evaluating
objects. Thus, you need to use a different Python API.

Mark.

Barry Scott wrote in message
<923433832.14002.0.nnrp-07.9e982a40@news.demon.co.uk>...
>What I want to do is call python to run python code and return the results
>to
>my app. But I cannot find anyway to do this.
>
>It would seem that I should be using PyRun_String to run a piece of python
>and return a result object to me. But I can only get a return value of
None.
>NULL is returned if the code does not run.
>
>How do I get python to return 2 to me when I ask it what 1+1 is?
Embedding python question: PyRun_String only returns None not what I calculate [ In reply to ]
This is not the most convenient solution to your problem, but may
be useful in your case.

When you embed Python in an application, the application often
exposes functions that are callable from Python scripts. You could
provide a function named setReturnValue(value), which when called,
passed a Python object (the value). The script calls this
function, and then, when it exits, the embedding application (the
caller of PyRun_String or PyRun_SimpleString) uses the Python value
saved by this function.

My application needs to do something similar. It needs to be able
to evaluate scripts, passing in one or more values and to "catch" a
value returned by the script. Effectively, we want scripts to be
functions. If you find a smoother way to do this than what I
described above, please let me know.

- Dave

Barry Scott <barry@scottbb.demon.co.uk> wrote:
> What I want to do is call python to run python code and return the results
> to
> my app. But I cannot find anyway to do this.

> It would seem that I should be using PyRun_String to run a piece of python
> and return a result object to me. But I can only get a return value of None.
> NULL is returned if the code does not run.

> How do I get python to return 2 to me when I ask it what 1+1 is?

> Below is a fragment of code that I'm using to investigate PyRun_String.

> BArry

> //
> // Test getting an object back from a command
> //
> char *command = "1+1";

> PyObject *m = PyImport_AddModule("__main__");
> if (m == NULL)
> return EXIT_FAILURE;

> PyObject *d = PyModule_GetDict(m);
> PyObject *v = PyRun_String(command, Py_file_input, d, d);
> if (v == NULL)
> {
> PyErr_Print();
> return EXIT_FAILURE;
> }

> PyObject *result = PyObject_Str( v );

> char *string = PyString_AsString( result );
> printf("Python returned: %s", string );

> Py_DECREF(result);
> Py_DECREF(v);
Embedding python question: PyRun_String only returns None not what I calculate [ In reply to ]
>Hopefully someone with more time can give a better answer...
>
>But the short story is that PyRun_String works with statements, not
>expressions. You can use it to, eg, import modules, and even call
>functions, but you cant get the result.

I guess I'll have to try and understand how the interactive python shell
works. (I got lost in the details last time...)

This whole area is in need of docs or extra comments in the sources.

Barry
Embedding python question: PyRun_String only returns None not what I calculate [ In reply to ]
G. David Kuhlman wrote in message ...
>This is not the most convenient solution to your problem, but may
>be useful in your case.
>
>When you embed Python in an application, the application often
>exposes functions that are callable from Python scripts. You could
>provide a function named setReturnValue(value), which when called,
>passed a Python object (the value). The script calls this
>function, and then, when it exits, the embedding application (the
>caller of PyRun_String or PyRun_SimpleString) uses the Python value
>saved by this function.

I thought of that buts its not a help accept in a few cases.


BArry