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

Dan Eloff dan.eloff at gmail.com
Wed Oct 29 21:53:47 CET 2008

On Wed, Oct 29, 2008 at 3:29 PM, Alex Mohr <amohr at pixar.com> wrote:
> The documentation isn't great, but the name 'borrowed' is a hint.  Using
> borrowed instructs boost.python that the PyObject * is a borrowed reference.
>  So you can use:
> object(handle<>(borrowed(ptr)))
> for when ptr is a borrowed reference and
> object(handle<>(ptr))
> for when ptr is a new reference.

That makes a lot more sense. What confused me is this from Dave
Abrahams in a message from Oct 07, I guess he got them mixed up.

If the is not pre-incremented for you, then

        boost::python::handle<> h((PyObject*)callbackID));
        return boost::python::object o(h);

would be correct.  Otherwise, you need

        return boost::python::object o(h);

btw, I notice that object(borrowed(ptr)) compiles and runs without
error. Is handle<>(...) superfluous when using borrowed?


More information about the Cplusplus-sig mailing list