[C++-sig] boost::python constructing object from PyObject *

Hans Meine hans_meine at gmx.net
Wed Oct 29 21:37:55 CET 2008


On Mittwoch 29 Oktober 2008, Dan Eloff wrote:
> When creating an object from a PyObject *, how do you distinguish
> between a PyObject pointer that is a new reference (must not be
> increfed, but must be decrefed) versus a PyObject * that is a borrowed
> reference? (should be increfed and decrefed)
>
> A very simple question for which there is no documented answer,
> conflicting answers in the archives of this mailing list, and hours of
> googling has turned up conflicting information.
>
> The best I could find was object(borrowed(ptr)) for new references and
> object(handle<>(ptr)) for borrowed pointers. I'm not sure if that is
> accurate, but if so that deserves a nomination for a terrible
> interface award.
>
> Please someone put me out of my misery. What's the interface for this?

IIRC I have complained about this in the past, too.  I think it was actually 
discussed in the BPL tutorial (and only there AFAIK), but was eventually 
deleted (probably because it was not the best place for that kind of 
information).

Actually, I have the same question as you, and the same impression that the 
above two ways are the "correct" ways.  Err, no - you did swap the two, 
right?

Looking back at my code, I think I used boost::python::detail::new_reference.

And for turning a Foo* pointer to an object of an exported class Foo, I used 
something as ugly as:
bp::detail::new_reference(typename manage_new_object::apply<T *>::type()(p)

AFAICS, I repeated my questions a total of three times (in the "How about 
class_<...>.enable_copy() ?" thead), but nobody answered so far.

-- 
Ciao, /  /                                                    .o.
     /--/                                                     ..o
    /  / ANS                                                  ooo
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20081029/679f8c36/attachment.pgp>


More information about the Cplusplus-sig mailing list