Embedding Python - Deleting a class instance
Jeff Epler
jepler at unpythonic.net
Tue Jun 21 08:33:36 EDT 2005
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);
}
/*------------------------------------------------------------------------*/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20050621/307b46bf/attachment.sig>
More information about the Python-list
mailing list