51 lines
1.3 KiB
Plaintext
51 lines
1.3 KiB
Plaintext
From: barry at scottbb.demon.co.uk (Barry Scott)
|
|
Date: Tue, 6 Apr 1999 22:22:17 +0100
|
|
Subject: Embedding python question: PyRun_String only returns None not what I calculate
|
|
Message-ID: <923433832.14002.0.nnrp-07.9e982a40@news.demon.co.uk>
|
|
X-UID: 767
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
|