This must be a common question, yet I can't find an answer to it anywhere.
I have embedded Python in a C++ app. All I want to be able to do is give some Python/C API function some python code in the form of a string, and get a PyObject that represents the result of evaluating that code.
So for instance...
char * code = "2+2";
PyObject * v = PyRun_String(code, ...);
if(PyInt_Check(v)) cout << PyInt_AsLong(v) << endl;
Ideally, this would print out "4"... unfortunately, it the result v is always either NULL (if an exception occurred) or NoneType.
No matter what I do or where I look, I can't figure out how to get the answer "4" from evaluating "2+2"! Is this even possible? I am beginning to think it isn't.
I have embedded Python in a C++ app. All I want to be able to do is give some Python/C API function some python code in the form of a string, and get a PyObject that represents the result of evaluating that code.
So for instance...
char * code = "2+2";
PyObject * v = PyRun_String(code, ...);
if(PyInt_Check(v)) cout << PyInt_AsLong(v) << endl;
Ideally, this would print out "4"... unfortunately, it the result v is always either NULL (if an exception occurred) or NoneType.
No matter what I do or where I look, I can't figure out how to get the answer "4" from evaluating "2+2"! Is this even possible? I am beginning to think it isn't.