Injecting instance of object into main module
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?
You're passing a null object pointer, so BPL correctly fails an
assertion check?
Niall
On 28 May 2012 at 21:39, Adam Preble wrote:
> 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?
>
--
Technology & Consulting Services - ned Productions Limited.
http://www.nedproductions.biz/. VAT reg: IE 9708311Q.
Work Portfolio: http://careers.stackoverflow.com/nialldouglas/
The object was declared in the class and instantiated when an instance of that object is created. It wasn't a pointer I failed to construct. I managed to screw around with the Python documentation awhile and get something like this working: boost::python::handle<> mainHandle(boost::python::borrowed(PyImport_AddModule("__main__"))); boost::python::object mainModule(mainHandle); boost::python::scope mainScope(mainModule); boost::python::object main_namespace = mainModule.attr("__dict__"); boost::python::object scripting_module( (handle<>(PyImport_ImportModule("scripting_synchronization"))) ); mainModule.attr("runtime") = ptr(&embeddedSync); So it popped up in my main namespace as an object named "runtime." On Tue, May 29, 2012 at 12:03 PM, Niall Douglas <s_sourceforge@nedprod.com>wrote:
You're passing a null object pointer, so BPL correctly fails an assertion check?
Niall
participants (2)
-
Adam Preble -
Niall Douglas