John Dean wrote: > PyRun_String("def title();", Py_file_input, dict, dict); > PyRun_String("\treturn 'Foo Bar'", Py_file_input, dict, dict); > PyRun_String("x = title()", Py_file_input, dict, dict); > PyObject * result = PyRun_String("print x", Py_file_input, dict, dict); > printf( "The result is %s\n", PyObject_AsString( result ); > > Each line throws an error.
Please don't paraphrase your code when asking questions such as this. The code above doesn't even compile (unmatched parentheses) so any response must be a guess as to which parts were wrong in your original code and which errors you introduced just for the posting. Each call to PyRun_String is independant. You seem to think you can call the function with part of your source code and then continue with more of the source in another call. You cannot. Try concatenating the lines together to at least make complete statements. Also make sure to handle the result from every call to PyRun_String: check it for null, if it is null you should format and print the error message. The following is your code rewritten as a working program. When you run it, it will tell you about the syntax error in your Python code. Fix that and you will see your program output. #include <python.h> void run() { PyObject *dict = PyDict_New(); PyObject *result; if (dict==NULL) { PyErr_Print(); return; } result = PyRun_String("def title();\n\treturn 'Foo Bar'\n", Py_file_input, dict, dict); if (result==NULL) { PyErr_Print(); return; } else { Py_DECREF(result); } result = PyRun_String("x = title()", Py_file_input, dict, dict); if (result==NULL) { PyErr_Print(); return; } else { Py_DECREF(result); } result = PyRun_String("print x", Py_file_input, dict, dict); if (result==NULL) { PyErr_Print(); return; } else { PyObject *rString = PyObject_Str(result); if (rString==NULL) { Py_DECREF(result); PyErr_Print(); return; } printf( "The result is %s\n", PyString_AsString(rString)); Py_DECREF(rString); Py_DECREF(result); } } int main(int argc, char **argv) { Py_Initialize(); run(); } -- http://mail.python.org/mailman/listinfo/python-list