[Python-Dev] another Py_TPFLAGS_HEAPTYPE question

Joshua Haberman joshua at reverberate.org
Mon Aug 17 00:08:55 CEST 2009

I wrote to this list a few weeks ago asking about Py_TPFLAGS_HEAPTYPE
(http://thread.gmane.org/gmane.comp.python.devel/105648).  It occurred
to me today that I could probably make object instances INCREF and
DECREF my type appropriately, without setting Py_TPFLAGS_HEAPTYPE, by
writing my own tp_alloc and tp_dealloc functions.  My tp_alloc function
could be:

PyObject *my_tp_alloc(PyTypeObject *type, Py_ssize_t nitems)
  PyObject *obj = PyType_GenericAlloc(type, nitems);
  if(obj) Py_INCREF(type);
  return obj;

This seems right since it is PyType_GenericAlloc that contains this

  if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)

I don't want to set Py_TPFLAGS_HEAPTYPE, but I want to get that
Py_INCREF(), so far so good.

But I couldn't find the corresponding Py_DECREF() in typeobject.c to
the above Py_INCREF().  Notably, object_dealloc() does not call
Py_DECREF(self->ob_type) if self->ob_type has the Py_TPFLAGS_HEAPTYPE
flag set.

So where does the Py_DECREF() for the above Py_INCREF() live?  I
expected to find this code snippet somewhere, but couldn't:

  if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)


More information about the Python-Dev mailing list