[C++-sig] boost::python embedding return value
Stefan Seefeld
seefeld at sympatico.ca
Thu Mar 4 18:19:18 CET 2010
On 03/04/2010 11:59 AM, Neal Becker wrote:
> int main () {
> Py_Initialize();
>
> object main_module = import("__main__");
> object main_namespace = main_module.attr("__dict__");
>
> try {
> object result = exec ("import sys\n"
> "sys.path.append('./')\n"
> "import test_embed\n"
> "test_embed.five_square()\n",
> main_namespace);
> int five_squared = extract<int> (result);
> std::cout<< five_squared<< '\n';
> }
> catch (error_already_set const&) {
> PyErr_Print();
> }
> }
>
>
> test_embed.py:
> --------------------
> def five_square ():
> return 5 ** 2
>
> I get:
> ./test_embed
> TypeError: No registered converter was able to produce a C++ rvalue of type
> int from this Python object of type NoneType
>
> Why did exec return None? I expected it to return the result of
> "test_embed.five_squared()", which is the int 25. What is the meaning of
> the return of exec_file? A python module can't return a result.
>
This is a straight wrapping of Python's C API. AFAICT, the return value
is only useful to determine whether the call was successful. Thus, None
may indicate an internal error, which you can check for with
PyErr_Occured(). (I'm actually surprised we don't catch this internally
and then raise an err_already_set exception !)
The real error could be that the interpreter wasn't able to import one
of the modules (wrong PYTHONPATH ?), or something similar.
Regards,
Stefan
--
...ich hab' noch einen Koffer in Berlin...
More information about the Cplusplus-sig
mailing list