[C++-sig] Boost Python - How to export boost::shared_ptr<std::string>

Romain CHANU romainchanu at gmail.com
Tue Dec 15 10:14:44 CET 2009


I have seen this page before but I have also never tried as I was looking
for a fast solution.

I was thinking to use boost::shared_ptr because I want to do something like:

// Python code (mystring is boost::shared_ptr<std::string>)

if object.mystring:

     print 'my string: ', object.mystring

Knowing that:

- If boost::shared_ptr attribute is not initialized, its value is 0)
- I use add_property


Any workaround?

Cheers,


2009/12/15 Roman Yakovenko <roman.yakovenko at gmail.com>

> On Tue, Dec 15, 2009 at 9:36 AM, Romain CHANU <romainchanu at gmail.com>
> wrote:
> > Hi,
> >
> > I want to export an attribute which is a boost::shared_ptr<std::string>
> and
> > I got the following error:
> > " TypeError: No Python class registered for C++ class class
> > boost::shared_ptr<class std::basic_string<char,struct
> > std::char_traits<char>,class std::allocator<char> > >  "
> > I found an old post on a similar issue :
> >
> http://old.nabble.com/Boost.python---How-to-export-shared_ptr%3Cvector%3Cshared_ptr%3Cstring%3E-%3E-%3E--tt22389131.html#a22389131
> > but no one has replied :-\
> >
> > Can anyone tell me how can I export this type?
>
> If I were you, I would not export it. In any case Python strings are
> immutable and you always returns a copy of the string.
>
> Instead of exposing that attribute directly create a get/set functions
> which use "raw" "std::string" in their interface and expose them.
>
> May be "custom converter" could help you (
>
> http://www.boost.org/doc/libs/1_41_0/libs/python/doc/v2/faq.html#custom_string
> ), but I never tried it on such use case.
>
>
>
> --
> Roman Yakovenko
> C++ Python language binding
> http://www.language-binding.net/
> _______________________________________________
> Cplusplus-sig mailing list
> Cplusplus-sig at python.org
> http://mail.python.org/mailman/listinfo/cplusplus-sig
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20091215/40c7252c/attachment.htm>


More information about the Cplusplus-sig mailing list