On Thu, Jul 15, 2010 at 10:38 AM, Robert Kern <[email protected]> wrote: > What are the recommendations for the use of the "object" type versus "PyObject > *"? Ondrej pointed out a potential bug in my line_profiler package: > > > http://github.com/certik/line_profiler/commit/dc3ad198f8a810d34da5da71ffccf01c5e05a161 > > I have a cdef function that serves as the callback for the PyEval_SetTrace() > functionality. It has a PyObject* argument that is sometimes NULL. This > argument > happens to be unused. In the version of Cython I originally developed > line_profile under , no code referenced this argument. In the development > version of Cython that Ondrej used to compile line_profiler, a Py_INCREF is > apparently generated. When the argument is NULL, this obviously fails. > > Is this intentional? Should "object" be avoided when the argument could > possibly > be NULL? Or should the generated code use Py_XINCREF/Py_XDECREF instead to > avoid > failure?
Typically PyObject* is used for borrowed references (i.e there's no need for you do decref it when you're done with it) whereas objects is used when you have ownership of a reference and need to handle the decref at the end. As for NULL, it is fine in the former (though be careful to use the Py_X... functions if necessary) but not in the latter. In this callback, the argument should be declared to take a PyObject*, and if you need to use it, cast it to an object if it's not NULL (maybe using None if it is). - Robert _______________________________________________ Cython-dev mailing list [email protected] http://codespeak.net/mailman/listinfo/cython-dev
