question with embeding python in C

Alex Martelli aleax at aleax.it
Wed Sep 12 08:26:04 EDT 2001


"LAMY Bertrand" <b_lamy at club-internet.fr> wrote in message
news:3B9EFC71.A90FD092 at club-internet.fr...
> here is my question :
> when I create a Python object in a C function with PyObject_New and the
> C function return that object to python, do I have to Py_INCREF the
> object before ?

No, PyObject_New returns an object with one reference to it
already, and you can just pass that reference on.

> In the help examples, for all 'new' functions that create in C an
> instance of a new type, the object is returned without Py_INCREF. that
> works.

Right.

> the 2 bugs were corrected by Py_INCREF the object before returning it.

So you have some OTHER refcount-related bug in these cases -- you
have one decref somewhere that you shouldn't have, so adding one
incref balances things out.  But you should really find out where
the stray decref comes from.  Since they're C objects of a type
you control, why not just add some printf calls in their destructors
to let you know the instant the dtor is running, and run the
simplest failing-case Python script you can imagine with tracing
turned on -- that should let you help narrow things down (you
can also use a C-level debugger to put a breakpoint in the
destructor and check the stack trace for more information, if
that's feasible on your specific platform).


Alex






More information about the Python-list mailing list