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

Simon Pickles sipickles at hotmail.com
Sun Jun 3 20:35:23 CEST 2007


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




More information about the Cplusplus-sig mailing list