I wrote the following module to test the behavior of PyInstance_New. I called it something like this: import vedel
class k: def __del__(self): print "deleted" vedel.g(k) I get output like: after creation, x->refcnt = 1 doing decref deleted after decref Unless there's a cycle and GC gets involved, all there is to deleting *anything* in Python is correctly managing the refcount. On the other hand, you can never free an object while it is still reachable. Some local name "x" may never spontaneously lose the thing it refers to. /*------------------------------------------------------------------------*/ #include <Python.h> static PyObject *g(PyObject *s, PyObject *o) { PyObject *x = PyInstance_New( o, NULL, NULL); if(x) { printf("after creation, x->refcnt = %d\n", x->ob_refcnt); printf("doing decref\n"); Py_DECREF(x); printf("after decref\n"); } else { printf("x == NULL\n"); } Py_INCREF(Py_None); return Py_None; } static PyMethodDef methods[] = { {"g", (PyCFunction)g, METH_O, NULL}, {NULL}, }; void initvedel(void) { Py_InitModule("vedel", methods); } /*------------------------------------------------------------------------*/
pgp85hc39n0gv.pgp
Description: PGP signature
-- http://mail.python.org/mailman/listinfo/python-list