Embedding python question: PyRun_String only returns None not what I calculate

Barry Scott barry at scottbb.demon.co.uk
Tue Apr 6 17:22:17 EDT 1999


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);






More information about the Python-list mailing list