Boost logo

Boost Users :

Subject: [Boost-users] Boost Python: add_property and shared_ptr
From: Raul Durand (raul.durand_at_[hidden])
Date: 2012-07-28 23:52:52


Hi!

I've been using the boost::python library for some time with few problems
but now I got stuck.
Fortunately I isolated the problem.

In the class Domain below I defined a property called solver which returns
a shared_ptr<Solver> instance:

*namespace bp=boost::python;**
**class Solver
**{
**public:
** bool _b;
** void func() { _b = true; }
**};**
**typedef shared_ptr<Solver> Solver_ptr;**
**
class Domain
**{
**public:
** Solver_ptr solver;
** void set_solver(Solver_ptr s) { solver = s; }
**};**
**typedef shared_ptr<Domain> Domain_ptr;**
**
BOOST_PYTHON_MODULE(test) {**
*

* bp::class_<Solver, Solver_ptr>("Solver")*

* .def("func", &Solver::func)*

* ;*

* bp::class_<Domain, Domain_ptr>("Domain")*

* .def("set_solver" , &Domain::set_solver)*

* .add_property("solver" , &Domain::solver)*

* ;*

*}*

The code compiles ok. The problem appears while running in Python:

*from test import *
**d = Domain()
**s = Solver()
**d.set_solver(s) # is it ok to do that?
**d.solver.func() # this line does not work*

With the following error message:

*Traceback (most recent call last):*
* File "test_test.py", line 5, in <module>*
* d.solver.func()*
*Boost.Python.ArgumentError: Python argument types in*
* Solver.func(Solver)*
*did not match C++ signature:*
* func(Solver {lvalue})*

I suspect that the problem is related with add_property returning a
shared_ptr<Solver> aka Solver_ptr.
It seems that add_property is returning a *non lvalue* Solver object.
I tried applying return value policies with no luck.
How can I get Domain::solver with type Solver_ptr in Python as a lvalue ?

Thanks!

Raul Durand



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net