[C++-sig] Getting object for PyObject (reference counting)

Alex Mohr amohr at pixar.com
Fri Feb 27 17:34:50 CET 2009

Murray Cumming wrote:
> I can't find any definitive documentation that tells me how I should get
> a boost::python::object to wrap an existing PyObject*. I guess that
> there's a way to do it that uses an existing reference, and a way that
> takes an extra reference.

You can construct a boost::python::object with a 
boost::python::handle<>.  boost::python::handle<> is sort of a "smart" 
PyObject *.  It manages the python object reference count automatically. 
  When you create a boost::python::handle<> you can tell it whether to 
bump the reference count or not.  Given a PyObject *p:

// Use with a "new reference" -- *doesn't* bump ref count.

// Use with a "borrowed reference" -- *does* bump ref count.


More information about the Cplusplus-sig mailing list