trouble using vector of shared_ptr using boost python

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

trouble using vector of shared_ptr using boost python

helferthomas
Hi,

In the following test, we wrap a class "A" (and the class shared_ptr<A> using boost python facilites) and a function returning a vector of shared_ptr of this class (see test.cxx). The class "A" provides a display method. Creating an object of type "A" and calling the display method just works as expected.

The trouble (see test.py) is that calling the "display" method when iterating over the elements of the vector (of shared_ptr...) leads to the following message :

Traceback (most recent call last):
  File "test.py", line 5, in <module>
    i.display()
Boost.Python.ArgumentError: Python argument types in
    A.display(A)
did not match C++ signature:
    display(A {lvalue})

Can anybody tell me if I misused the library ?

Sincerely,

Helfer Thomas

P.S. : This has been tested on Debian squeeze using python 2.6, boost 1.46 and 1.48.
_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig

test.cxx (1K) Download Attachment
test.py (92 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: trouble using vector of shared_ptr using boost python

Jim Bosch-2
On 01/13/2012 03:13 AM, [hidden email] wrote:

> Hi,
>
> In the following test, we wrap a class "A" (and the class shared_ptr<A>  using boost python facilites) and a function returning a vector of shared_ptr of this class (see test.cxx). The class "A" provides a display method. Creating an object of type "A" and calling the display method just works as expected.
>
> The trouble (see test.py) is that calling the "display" method when iterating over the elements of the vector (of shared_ptr...) leads to the following message :
>
> Traceback (most recent call last):
>    File "test.py", line 5, in<module>
>      i.display()
> Boost.Python.ArgumentError: Python argument types in
>      A.display(A)
> did not match C++ signature:
>      display(A {lvalue})
>
> Can anybody tell me if I misused the library ?
>

It looks like vector_indexing_suite isn't smart enough to figure out it
shouldn't return a proxy object for a vector of shared_ptr.

This snippet should solve your problem:

class_<vector<shared_ptr<A> > >("AVector")
   .def(vector_indexing_suite<vector<shared_ptr<A> >, true >());
                                                      ^^^^

That sets the vector_indexing_suite's NoProxy template parameter to
"true".  I believe those proxies only exist to keep the container's
elements from becoming dangling references in Python, which isn't
necessary for containers of shared_ptr.

Jim
_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Reply | Threaded
Open this post in threaded view
|

Re: trouble using vector of shared_ptr using boost python

helferthomas
Thanks, it works flawlessly.

Sincerly,

Helfer Thomas

----- Mail original -----
De: "Jim Bosch" <[hidden email]>
À: [hidden email]
Envoyé: Vendredi 13 Janvier 2012 17:15:27
Objet: Re: [C++-sig] trouble using vector of shared_ptr using boost python

On 01/13/2012 03:13 AM, [hidden email] wrote:

> Hi,
>
> In the following test, we wrap a class "A" (and the class shared_ptr<A>  using boost python facilites) and a function returning a vector of shared_ptr of this class (see test.cxx). The class "A" provides a display method. Creating an object of type "A" and calling the display method just works as expected.
>
> The trouble (see test.py) is that calling the "display" method when iterating over the elements of the vector (of shared_ptr...) leads to the following message :
>
> Traceback (most recent call last):
>    File "test.py", line 5, in<module>
>      i.display()
> Boost.Python.ArgumentError: Python argument types in
>      A.display(A)
> did not match C++ signature:
>      display(A {lvalue})
>
> Can anybody tell me if I misused the library ?
>

It looks like vector_indexing_suite isn't smart enough to figure out it
shouldn't return a proxy object for a vector of shared_ptr.

This snippet should solve your problem:

class_<vector<shared_ptr<A> > >("AVector")
   .def(vector_indexing_suite<vector<shared_ptr<A> >, true >());
                                                      ^^^^

That sets the vector_indexing_suite's NoProxy template parameter to
"true".  I believe those proxies only exist to keep the container's
elements from becoming dangling references in Python, which isn't
necessary for containers of shared_ptr.

Jim
_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig
_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig