En Tue, 01 Apr 2008 18:49:28 -0300, Mitko Haralanov <[EMAIL PROTECTED]> escribió:
> For the most part, I understand the theory behind reference counting in > Python C code. But there is one thing that I am confused about and I > have not been able to clear it up. > > Let say that you have the following function (over-simplified): > > PyObject *do_work (PyObject *self, PyObject *args) { > PyObject *new_obj; > new_obj = PyDict_New (); > return new_obj; > } > > What I don't get is whether I have to decrement the reference to > new_obj before I return it. I know that function that return object are > giving away the reference but I am still confused. PyDict_New returns a new reference (check section 7.4.1 in the API Reference), so do_work "owns" that reference. Functions that are intended to be called from Python code must return an owned reference to some PyObject* (see section 1.10.2 Ownership Rules, in the Extending/Embedding reference) (yes, these things are scattered all over the place...) Since do_work has to return an owned reference, it can't decrement it. In this example it's rather clear because the *only* reference to the newly created dict is hold by the function, and decrementing it would destroy the dictionary. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list