[C++-sig] problem w/intermodule communication

William Ladwig wladwig at wdtinc.com
Tue Nov 25 23:42:20 CET 2008


Neal,

Try adding:

register_ptr_to_python< boost::shared_ptr<A> >();

to your BOOST_PYTHON_MODULE(test2) section.

Hope this helps,

Bill

________________________________________
From: cplusplus-sig-bounces+wladwig=wdtinc.com at python.org [cplusplus-sig-bounces+wladwig=wdtinc.com at python.org] On Behalf Of Neal Becker [ndbecker2 at gmail.com]
Sent: Monday, November 24, 2008 6:15 PM
To: cplusplus-sig at python.org
Subject: [C++-sig] problem w/intermodule communication

Here is a simple example.

---------------
test1.cc

#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/class.hpp>
#include <boost/python/init.hpp>

using namespace boost::python;

struct A {
  A (int _x) : x (_x) {}
  int x;
};

BOOST_PYTHON_MODULE(test1) {
  class_<A> ("A", init<int>());
}
----------------------

test2.cc

#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/class.hpp>
#include <boost/python/init.hpp>
#include <boost/shared_ptr.hpp>

using namespace boost::python;

struct A {
  A (int _x) : x (_x) {}
  int x;
};

struct B {
  B (boost::shared_ptr<A> _a) : a (_a) {}
  boost::shared_ptr<A> a;
};

BOOST_PYTHON_MODULE(test2) {
  class_<B> ("B", init<boost::shared_ptr<A> >())
    .def_readonly ("a", &B::a)
     ;
}
-------------------------

test12.py

from test1 import A
from test2 import B

a = A(1)
b = B(a)

print b.a
---------------------

TypeError: No to_python (by-value) converter found for C++ type: boost::shared_ptr<A>

How can I solve this?  Now consider:

------------------------
test3.cc

#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/class.hpp>
#include <boost/python/init.hpp>

using namespace boost::python;

struct A {
  A (int _x) : x (_x) {}
  int x;
};

struct B {
  B (boost::shared_ptr<A> _a) : a (_a) {}
  boost::shared_ptr<A> a;
};

BOOST_PYTHON_MODULE(test3) {
  class_<A> ("A", init<int>());

  class_<B> ("B", init<boost::shared_ptr<A> >())
    .def_readonly ("a", &B::a)
     ;
}

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

test13.py

from test3 import A, B

a = A(1)
b = B(a)

print b.a
---------------------------
<test3.A object at 0x2167208>

So if both are in same module, no problem.  If they are in different modules it doesn't work.  Any ideas?

_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig at python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig


More information about the Cplusplus-sig mailing list