[C++-sig] Same opaque pointer used in 2 modules provoques an assertion error

David Abrahams dave at boost-consulting.com
Tue Jul 5 17:59:22 CEST 2005


"Yves Secretan" <Yves_Secretan at inrs-ete.uquebec.ca> writes:

> Hi,
>
> I have a container of pointers and would like to add pointers to this
> container from Python using an opaque pointer.
> Everything works fine if I have only one module, but if I split the code in
> more modules, I get a failed assertion message:
> File: 
registry.cpp
> Line: 161
> Expression: slot ==0
> and ignoring the exception:
> test.py:11: RuntimeWarning: to-Python converter for struct void_ * already
> registered; second conversion method ignored.
>
> My understanding is that each module tries to register a converter, and I
> could not find a way to prevent it.
>
> My environnement: boost-1.32.0; Python 2.3; msvc7.0
>
> I join the simplest example I could build: data1.cpp is the first module;
> data2.cpp the second module; test.py should display the problem.
>
> Thanks for any help
> Yves

I think this is a bug in the return value policy.  Looking at it, each
time you use it in a new module the converter will try to register
itself.

What happens when you stop using the return value policy and instead
construct an opaque_pointer_converter<void_*>("void_") in the first of
the two modules to be loaded?

Gottfried,
http://www.boost.org/libs/python/doc/v2/opaque_pointer_converter.html
refers me to
http://www.boost.org/libs/python/doc/v2/return_opaque_pointer.html#example
for an example, but there's no example of the use of
opaque_pointer_converter there.  What should we do about this?

> ---------------------------------------------------------------
> Yves Secretan, Professeur
> INRS-ETE
> 490, rue de la Couronne
> Québec, Québec
> G1K 9A9 CANADA
>
> tel: (418) 654 38 48   fax: (418) 654 26 00
> e-mail: Yves_Secretan at ete.inrs.ca
> ---------------------------------------------------------------
>
>
> #include <boost/python.hpp>
> #include <iostream>
>
> struct container
> {
> 	void add(void* p) { std::cerr << "Adding " << p << std::endl; }
> };
>
> struct data1
> {
> };
>
> struct void_;
> BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(void_);
>
> void container_add(container& c, void_* p) 
> {
> 	c.add((void*) p); 
> }
>
> void_* data1_asVoid(data1& d) 
> {
> 	return (void_*)(&d); 
> }
>
>
> BOOST_PYTHON_MODULE(data1)
> {
>    using namespace boost::python;
>
>    class_< container >("container")
>       .def("add", &container_add)
>    ;
>
>    class_< data1 >("data1")
>       .def("asVoid", &data1_asVoid, return_value_policy< return_opaque_pointer >())
>    ;
>
> }
>
>
> #include <boost/python.hpp>
>
> struct data2
> {
> };
>
> struct void_;
> BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(void_);
>
> void_* data2_asVoid(data2& d) 
> {
> 	return (void_*)(&d); 
> }
>
>
> BOOST_PYTHON_MODULE(data2)
> {
>    using namespace boost::python;
>
>    class_< data2 >("data2")
>       .def("asVoid", &data2_asVoid, return_value_policy< return_opaque_pointer >())
>    ;
>
> }
>
>
>
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com




More information about the Cplusplus-sig mailing list