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

David Abrahams dave at boost-consulting.com
Wed Jun 18 22:59:52 CEST 2003

"Jacques A. Vidrine" <nectar-pycpp at celabo.org> writes:

> 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).

And why does that confuse you?  That seems to show appropriate
solutions to the question you posed.

> (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>.)

A fine, fine idea.

>> 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.

Well, given that you're not actually dealing with std::string, the
above advice is now wrong.

Dave Abrahams
Boost Consulting

More information about the Cplusplus-sig mailing list