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...

_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig

Reply via email to