[C++-sig] using extract with embedded boost::python

Simon Pickles sipickles at hotmail.com
Sun Jun 3 20:52:48 CEST 2007


Sorry, A cut-n-paste error, the code which works is the boost example:

object main_module((
     handle<>(borrowed(PyImport_AddModule("__main__")))));

object main_namespace = main_module.attr("__dict__");

handle<> ignored((PyRun_String(

    "result = 5 ** 2"

    , Py_file_input
    , main_namespace.ptr()
    , main_namespace.ptr())
));

int five_squared = extract<int>(main_namespace["result"]);


----------------------------------------------------------------





http://www.simonpickles.com  --- http://www.squirtualreality.com





>From: "Simon Pickles" <sipickles at hotmail.com>
>Reply-To: Development of Python/C++ integration <c++-sig at python.org>
>To: c++-sig at python.org
>Subject: [C++-sig] using extract with embedded boost::python
>Date: Sun, 03 Jun 2007 19:35:23 +0100
>
>
>Hi,
>
>I am getting an error when I run example code like this:
>
>      // Retrieve the main module.
>/*      object main = import("__main__");
>
>       // Retrieve the main module's namespace
>       object global(main.attr("__dict__"));
>
>       // Define greet function in Python.
>       object result = exec(
>         "def greet(self):               \n"
>         "   return 'Hello from Python!' \n",
>         global, global);
>
>       // Create a reference to it.
>       object greet = global["greet"];
>
>       // Call it.
>       std::string message = extract<std::string>(greet());
>
>It gives error_already_set, which I catch, but then it crashes on shutdown
>during garbage collection. The extract line is causing the crash.
>
>Strangely this works:
>
>         object main_module((
>      handle<>(borrowed(PyImport_AddModule("__main__")))));
>
>object main_namespace = main_module.attr("__dict__");
>
>handle<> ignored((PyRun_String( "import myPython" )));
>
>int five_squared = extract<int>(main_namespace["result"]);
>
>g_log->Log( "%d\n", five_squared );
>
>Can anyone demystify the difference to a boost:python newbie?
>
>Thanks
>
>Simon
>
>
>http://www.simonpickles.com  --- http://www.squirtualreality.com
>
>_________________________________________________________________
>Could you be the guest MSN Movies presenter? Click Here to Audition
>http://www.lightscameraaudition.co.uk
>
>_______________________________________________
>C++-sig mailing list
>C++-sig at python.org
>http://mail.python.org/mailman/listinfo/c++-sig

_________________________________________________________________
Txt a lot? Get Messenger FREE on your mobile. 
https://livemessenger.mobile.uk.msn.com/




More information about the Cplusplus-sig mailing list