[C++-sig] Injecting instance of object into main module

Adam Preble adam.preble at gmail.com
Tue May 29 04:39:30 CEST 2012


I wanted to provide an instance of a particular object I'm using for
Stackless Python state management.  I'm trying to take over the green
thread management so I can do my own scheduling--but that's only context.
 In the attached code, I'm trying to do some of life's more simple things.
 I have the main module, and the scope for that module.  With this, I had
hoped I could embed an actual instance of an object.  From there I
anticipate calling it from Python without having to import or do anything
else.  I figured I'd take the main script scope, add an object called
"runtime", and set it to the one in my C++ runtime.

It compiles fine but blows up during runtime with the ever-descriptive
"error_already_set()" without a message.

This problem only seems to touch the Boost part of the runtime so I don't
think that's an issue.  I'm using Stackless Python 2.6.5, with Boost 1.49,
on Windows Vista with Visual Studio 2010.

I had as my inspiration this:
http://wiki.python.org/moin/boost.python/HowTo#module_level_objects

Which doesn't look to be current.  I can't figure out how to use the
run-time example there, in particular.  It looks like calling interpreter()
is not supported, or I have no idea how to make it happen.

I'm doing something like this:
EmbeddedSynchronization embeddedSync;        // A class member up above
...

[Did the PyInitialize]
boost::python::handle<>
mainHandle(boost::python::borrowed(PyImport_AddModule("__main__")));
boost::python::object   mainModule(mainHandle);
boost::python::scope    mainScope(mainModule);

boost::python::object   class_oClassSync =
boost::python::class_<EmbeddedSynchronization,
boost::noncopyable>("EmbeddedSynchronization")
                                            .def("nap",
&EmbeddedSynchronization::beNice)
                                            .staticmethod("nap");

mainScope().attr("runtime") = embeddedSync;         // Dead right here

Stack trace going down in there:
> StacklessEmbedding.exe!boost::python::throw_error_already_set()  Line 62
C++
  StacklessEmbedding.exe!boost::python::expect_non_null<_object>(_object *
x)  Line 46 C++
  StacklessEmbedding.exe!boost::python::converter::detail::return_object_manager_from_python<boost::python::api::object>::operator()(_object
* obj)  Line 156 + 0x9 bytes C++
  StacklessEmbedding.exe!boost::python::call<boost::python::api::object>(_object
* callable, boost::type<boost::python::api::object> * __formal)  Line 76 +
0x10 bytes C++
  StacklessEmbedding.exe!boost::python::api::object_operators<boost::python::api::object>::operator()()
 Line 54 + 0x14 bytes C++

Anything obviously wrong?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20120528/3a9a1c94/attachment.html>


More information about the Cplusplus-sig mailing list