[Python-Dev] Py_BuildValue and decref

Mihai Ibanescu misa at redhat.com
Sat Sep 9 00:35:58 CEST 2006


On Fri, Sep 08, 2006 at 06:27:08PM -0400, Barry Warsaw wrote:
> 
> On Sep 8, 2006, at 6:06 PM, Mihai Ibanescu wrote:
> 
> >There is no description of what happens when Py_BuildValue fails.  
> >Will it
> >decref the python object passed in? Will it not?
> 
> I just want to point out that the C API documentation is pretty  
> silent about the refcounting side-effects in error conditions (and  
> often in success conditions too) of most Python functions.  For  
> example, what is the refcounting side-effects of PyDict_SetItem() on  
> val?  What about if that function fails?  Has val been incref'd or  
> not?  What about the side-effects on any value the new one replaces,  
> both in success and failure?

In this particular case, it doesn't decref it (or so I read the code).
Relevant code is in do_mkvalue from Python/modsupport.c

                case 'N':
                case 'S':
                case 'O':
                if (**p_format == '&') {
                        typedef PyObject *(*converter)(void *);
                        converter func = va_arg(*p_va, converter);
                        void *arg = va_arg(*p_va, void *);
                        ++*p_format;
                        return (*func)(arg);
                }
                else {
                        PyObject *v;
                        v = va_arg(*p_va, PyObject *);
                        if (v != NULL) {
                                if (*(*p_format - 1) != 'N')
                                        Py_INCREF(v);
                        }
                        else if (!PyErr_Occurred())
                                /* If a NULL was passed
                                 * because a call that should
                                 * have constructed a value
                                 * failed, that's OK, and we
                                 * pass the error on; but if
                                 * no error occurred it's not
                                 * clear that the caller knew
                                 * what she was doing. */
                                PyErr_SetString(PyExc_SystemError,
                                        "NULL object passed to
Py_BuildValue");
                        return v;
                }


Barry, where can I ship you my cloning machine? :-)

Misa


More information about the Python-Dev mailing list