Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieving the value of a Python expression

Status
Not open for further replies.

GR1EVER

Programmer
Mar 6, 2003
23
US
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.

:(
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top