Py_Object* Py_BuildValue, Py_INCREF necessary?

Tim Peters tim.peters at gmail.com
Mon Jan 17 22:05:04 CET 2005


[Torsten Mohr]
> when i write an extension module in C and return a Py_Object*
> that i've built with Py_BuildValue, do i need to use Py_INCREF
> on that before i return it to python from my extension module
> or not?

The docs for Py_BuildValue() say it returns a new reference (and it
does).  So the caller owns the reference, and is responsible for
either passing its reference on to someone else, or for giving up its
reference (via Py_DECREF).  If you pass the result on to your
extension module's caller, then you're also transferring ownership of
the reference to your extension module's caller (and should not do
Py_INCREF -- which would create yet another reference).



More information about the Python-list mailing list