On 13 November 2015 at 03:08, Xiang Zhang <18518281...@126.com> wrote: > I think the meaning of Py_INCREF a static type object is to prevent it from > being deallocated when it is Py_DECREFed somehow later. Just as you said, it > may be somehow deallocated when using. > > NoddyType is a static struct so I don't think it lives on Python's heap and > deallocating it is a wrong action. Just as I mentioned, type_dealloc seems > to only deallocated HEAPTYPE. And by the way, when NoddyType is created, it > has a reference count 1 with PyVarObject_HEAD_INIT. So if I don't Py_INCREF > it, when it is somehow Py_DECREDed later, it will reach reference count 0 > and fail the assert in type_dealloc. If it is Py_INCREFed, just like the > module holds a reference to it, it will at least have a reference count 1 > and never reach 0.
Other code shouldn't Py_DECREF it unless it owns a reference meaning that it has first called Py_INCREF. The only exception I can think is possibly that someone would do: >>> import noddy >>> del noddy.Noddy For that to work properly I guess that Noddy needs a reference count of 2: one for being a module attribute and an extra one to prevent it from ever being deallocated. -- Oscar -- https://mail.python.org/mailman/listinfo/python-list