[C++-sig] Boost.Python v2: reference-counting and user-friendly conversions

David Abrahams david.abrahams at rcn.com
Fri Jun 14 01:53:58 CEST 2002


----- Original Message -----
From: "Ralf W. Grosse-Kunstleve" <rwgk at yahoo.com>
To: <c++-sig at python.org>
Sent: Thursday, June 13, 2002 7:46 PM
Subject: Re: [C++-sig] Boost.Python v2: reference-counting and
user-friendly conversions


> > The interface for causing the reference count to be incremented has
> > changed: now one passes the argument through the borrow() function,
> > signaling that we are getting a borrowed reference:
> >
> >     handle<> x(borrow(PyTuple_GetItem(t, n)));
>
> "borrow" sounds like we are about to borrow something, when indeed
> we are "signaling that we are getting a borrowed reference."
> Therefore                                     ^^
>
>       handle<> x(borrowed(PyTuple_GetItem(t, n)));
>
> seems much more intuitive to me.

I had that thought myself, and will happily make the change if you don't
mind typing the extra two characters ;-)


> > So, what's the behavior when the user passes a raw PyObject*? It seems
to
> > me that we'd like the following to work:
> >
> >    object(PyObject_GetAttrString(some_PyObject_pointer,
> >"some_attribute_name"))
>
> Considering all the questions that follow, are you sure it is worth it?
> I'd only expect to be able to do this:
>
>      object(handle<>(PyObject_GetAttrString(
>        some_PyObject_pointer, "some_attribute_name")))

Hooray, consensus!

> If I understand correctly, eventually there will be a wrapper for
> PyObject_GetAttrString anyway, so being able to construct an object()
> from PyObject* directly as a convenience will not be interesting.

There's a small potential efficiency gain from not manipulating reference
counts, but not (IMO) worth it. The bare metal is always available if you
need it.

> > Right now, if you pass a PyObject* as an argument to call<>, we first
check
> > for NULL (in which case we manufacture a reference to None) then the
> > reference count is incremented before the arg_to_python converter takes
> > possession.
>
> If you'd make handle<> the only interface that accepts PyObject* you
> could avoid many of your questions. You could make the NULL -> None
> conversion explicit: in addition to borrowed() and allow_null() you
> could supply null_to_none().

Yep. Or, the user can write:

    p ? handle<>(p) : none()

Which is just fine.

Mind you, I still think NULL -> None makes sense for other pointers, e.g.
char*, MyWrappedClass*, ...

-Dave







More information about the Cplusplus-sig mailing list