PyEval_EvalCodeEx return value

Mateusz Loskot mateusz at
Fri Sep 23 05:54:43 EDT 2011

On 23/09/11 00:47, Mark Hammond wrote:
> On 20/09/2011 8:34 PM, Mateusz Loskot wrote:
>> I'm trying to dig out details about what exactly is the return
>> value the of PyEval_EvalCodeEx function in Python 3.x
>> The documentation is sparse, unfortunately.
>> Perhaps I'm looking at wrong function.
>> My aim is simple, I need to execute Python code using Python interpreter
>> embedded in my C++ application.
>> The Python code is a simple script that always returns single value.
>> For example:
>> #! /usr/bin/env python
>> def foo(a, b):
>> return a + b
>> f = foo(2, 3)
>> But, f can be of different type for different script: one returns
>> numeric value, another returns a sequence, so the type is not
>> possible to be determined in advance.
>> I know how to capture Python stdout/stderr.
>> I also know how to access the "f" attribute using
>> PyObject_GetAttrString and then I can convert "f" value to C++ type
>> depending on PyObject type.
>> However, I guess there shall be a way to access "f" value
>> directly from PyEval_EvalCode return object:
>> PyObject* evalRet = ::PyEval_EvalCode(...);
>> But, I can't find any details what the "evalRet" actually is.
> Eval is to eval an expression. If you simply eval the expression "f" in
> the context of the module you should get the result returned. Obviously
> though it is designed to eval more complex expressions and in your
> specific example, doing the getattr thing will also work fine.

Hi Mark,

So, the result of PyEval_EvalCode strictly depends on the code being 
evaluated. It makes sense.

Thanks for help!

Best regards,
Mateusz Loskot,
Charter Member of OSGeo,
Member of ACCU,

More information about the Python-list mailing list