On Wednesday 09 September 2009 02:00:36 Greg Ewing wrote: > Sebastien Binet wrote: > > object PyDict_Copy(object p) > > PyObject* PyDict_GetItem(object p, object key) > > > > are they just syntaxic sugar, sweeting the pot to the very same thing ? > > I think PyObject * has been used for PyDict_GetItem because it > returns a borrowed reference. > thanks, and as usual, moments after I hit the send button I found the answer in the code (python.pxd):
# For all the declaration below, whenver the Py_ function returns # a *new reference* to a PyObject*, the return type is "object". # When the function returns a borrowed reference, the return # type is PyObject*. When Cython sees "object" as a return type # it doesn't increment the reference count. When it sees PyObject* # in order to use the result you must explicitly cast to <object>, # and when you do that Cython increments the reference count wether # you want it to or not, forcing you to an explicit DECREF (or leak memory). # To avoid this we make the above convention. # Cython takes care of this automatically for anything of type object. ## More precisely, I think the correct convention for ## using the Python/C API from Pyrex is as follows. ## ## (1) Declare all input arguments as type "object". This way no explicit ## <PyObject*> casting is needed, and moreover Pyrex doesn't generate ## any funny reference counting. ## (2) Declare output as object if a new reference is returned. ## (3) Declare output as PyObject* if a borrowed reference is returned. sorry for the RTFM noise. cheers, sebastien. -- ######################################### # Dr. Sebastien Binet # Laboratoire de l'Accelerateur Lineaire # Universite Paris-Sud XI # Batiment 200 # 91898 Orsay ######################################### _______________________________________________ Cython-dev mailing list [email protected] http://codespeak.net/mailman/listinfo/cython-dev
