[C++-sig] Manage new object return policy (was Re: new to python; old to C++)

Paul Melis paul at pecm.nl
Mon Nov 10 19:59:02 CET 2008


David Abrahams wrote:
> on Sat Nov 01 2008, Paul Melis <paul-AT-pecm.nl> wrote:
>
>   
>> Gustavo Carneiro wrote:
>>     
>>>     There's an interesting question about whether it's better to use
>>>     boost.python or SWIG. I've been using boost.python for years, so I
>>>     have a lot invested in it, but if I were starting from scratch, I
>>>     might consider using SWIG because it gives you the flexibility to
>>>     generate wrappers for languages other than Python.
>>>
>>>
>>> On the other hand, SWIG generates ugly and inneficient code, at least
>>> for the Python case.
>>>       
>> I'd like to see proof of the claim that SWIG's wrapper code is
>> inefficient. In my experience it is not more inefficient than what, for
>> example, boost.python via Py++ provides.
>>     
>
> I doubt that SWIG generates less efficient code.  However, I would be
> surprised if SWIG's is as careful about dealing with lifetime and
> ownership issues that are crucial to writing Pythonic and un-crashable
> bindings as Boost.Python is
In an attempt to compare SWIG and Boost on a simple synthetic benchmark I've become stuck on the following:

I have a method that returns a new instance of class Value and returns a pointer to it. The caller is then responsible for freeing of that instance at a suitable moment later on. I specify the manage_new_object return value policy for this method (and another one which basically returns its result) and this compiles fine. But when I run a small test script I get

CallbackWrap::operator()
Have override for __call__, calling it
Traceback (most recent call last):
  File "t_boost.py", line 25, in <module>
    r = obj.go(v1, v2)
ReferenceError: Attempt to return dangling pointer to object of type: Value

The FAQ entry on this doesn't really help, so perhaps I'm not using the return policy correctly or missing something else. The full test code (which is actually quite small) is attached.

Thanks in advance for any help,
Paul

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: classes.h
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20081110/2c9b6374/attachment.txt>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: t_boost.py
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20081110/2c9b6374/attachment-0001.txt>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: boost.cpp
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20081110/2c9b6374/attachment-0002.txt>


More information about the Cplusplus-sig mailing list