[Python-Dev] Py_BuildValue and decref

Mihai Ibanescu misa at redhat.com
Sat Sep 9 00:06:05 CEST 2006


Hi,

Looking at:

http://docs.python.org/api/arg-parsing.html

The description for "O" is:

"O" (object) [PyObject *]
    Store a Python object (without any conversion) in a C object pointer. The
    C program thus receives the actual object that was passed. The object's
    reference count is not increased. The pointer stored is not NULL.

There is no description of what happens when Py_BuildValue fails. Will it
decref the python object passed in? Will it not?

Looking at tupleobject.h:

/*
Another generally useful object type is a tuple of object pointers.
For Python, this is an immutable type.  C code can change the tuple items
(but not their number), and even use tuples are general-purpose arrays of
object references, but in general only brand new tuples should be mutated,
not ones that might already have been exposed to Python code.

*** WARNING *** PyTuple_SetItem does not increment the new item's reference
count, but does decrement the reference count of the item it replaces,
if not nil.  It does *decrement* the reference count if it is *not*
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
inserted in the tuple.  Similarly, PyTuple_GetItem does not increment the
returned item's reference count.
*/

So, if the call to PyTuple_SetItem fails, the value passed in is lost. Should
I expect the same thing with Py_BuildValue?


Looking at how other modules deal with this, I picked typeobject.c:

        result = Py_BuildValue("[O]", (PyObject *)type);
        if (result == NULL) {
                Py_DECREF(to_merge);
                return NULL;
        }

so no attempt to DECREF type in the error case.


Further down...


                        if (n) {
                                state = Py_BuildValue("(NO)", state, slots);
                                if (state == NULL)
                                        goto end;
                        }

and further down:

  end:
        Py_XDECREF(cls);
        Py_XDECREF(args);
        Py_XDECREF(args2);
        Py_XDECREF(slots);
        Py_XDECREF(state);
        Py_XDECREF(names);
        Py_XDECREF(listitems);
        Py_XDECREF(dictitems);
        Py_XDECREF(copy_reg);
        Py_XDECREF(newobj);
        return res;

so it will attempt to DECREF the (non-NULL) slots in the error case.

It's probably not a big issue since if Py_BuildValue fails, you have bigger
issues than memory leaks, but it seems inconsistent to me. Can someone that
knows the internal implementation clarify one way over the other?

Thanks!
Misa


More information about the Python-Dev mailing list