garbage collection / cyclic references
"Martin v. Löwis"
martin at v.loewis.de
Sat Mar 21 09:51:53 CET 2009
> The actual backend of CPython requires garbage-collected container
> types to implement tp_inquiry and tp_clear methods, but user-defined
> types apparently aren't required to conform.
tp_inquiry doesn't exist, you probably mean tp_traverse. tp_traverse
is completely irrelevant for python-defined types; the VM can traverse
a user-defined type just fine even without the help of tp_traverse.
If a C-defined type fails to implement tp_traverse when it should,
then garbage collection breaks entirely.
tp_clear isn't invoked for an object at all if the object is in a
cycle with finalizers, so it's not something that you can use to
detect that you are in a cycle with finalizers.
Cycles with finalizers are considered a bug; application programmers
should check gc.garbage at the end of the program to determine whether
they have this bug. There is an easy design pattern around it, so I'm
-1 on complicating the GC protocol.
More information about the Python-list