[C++-sig] Re: returning auto_ptr, `No to_python converter' ?

Jacques A. Vidrine nectar-pycpp at celabo.org
Wed Jun 18 22:09:22 CEST 2003


On Wed, Jun 18, 2003 at 03:42:11PM -0400, David Abrahams wrote:
> See my reply to Jarda Gresula (just posted)...
> 
> That will help you understand things, but it won't solve your problem.
> My recommendations for Jarda cause auto_ptr<T> to be converted to a
> Python T object which refers to its C++ object through a copy of the
> auto_ptr object.
> 
> std::string is converted to Python as a regular Python string, and
> there's no way to get a Python string to use an auto_ptr to hold its
> storage.  

Actually, std::string was used just for the example case.  In
real-life, it is a non-copyable class T.  I'll have a look at the
solution you suggested for Jarda.

I'm confused because it appeared to me that what I was trying to
accomplish isn't so different from what is done in
libs/python/test/auto_ptr.cpp (only looked at source -- did not
actually try it).

(The interface for the classes I am wrapping changed from returning
T * to returning std::auto_ptr<T>.  I was previously using
return_value_policy<manage_new_object>.)

> I think you just want to register a custom to-python
> converter for std::auto_ptr<std::string>.  Something like:
> 
>     struct auto_ptr_string_to_python
>         : to_python_converter<simple, auto_ptr_string_to_python>
>     {
>         static PyObject* convert(std::auto_ptr<std::string> const& x)
>         {
>             return PyString_FromStringAndSize(x->c_str(), x->size());
>         }
>     };
> 
> Just constructing one of those in your module initialization function
> should handle it.

Thanks much!  By the way, Boost.Python is really cool.
Cheers,
-- 
Jacques Vidrine   . NTT/Verio SME      . FreeBSD UNIX       . Heimdal
nectar at celabo.org . jvidrine at verio.net . nectar at freebsd.org . nectar at kth.se




More information about the Cplusplus-sig mailing list