[issue9141] Allow objects to decide if they can be collected by GC

Kristján Valur Jónsson report at bugs.python.org
Sun Jul 4 19:56:21 CEST 2010

Kristján Valur Jónsson <kristjan at ccpgames.com> added the comment:

tp_del() is generally invoked by tp_dealloc().  See for example typeobject.c:849.  gc.collect() never invokes tp_del() directy, that is left for the objects themselves (as part of tp_dealloc())

Now, gc already knows that if tp_del is present, it cannot cause break the cycle, (for various technical reasons, one of them being that tp_del would be invoked with various dependency objects already nerfed by tp_clear()).

But this is not always enough:
1) it may be too pessimistic.  Sometimes the tp_del method doesn't do anything significant, or isn't even called, depending on runtime state, so it is safe for gc to clear the object (see for example genobject.c:30).
2) Sometimes finalization behaviour is not defined by a tp_del method at all, but rather some custom code in tp_dealloc.

All this patch does, is to generalize the mechanism already provided for genobject.c (by PyGen_NeedsFinalizing()), to any object: Any object can signal to gc that: a) it is ok to collect a cycle with me in it, or b) no, it is unsafe to do so.  With this patch in place, PyGen_NeedsFinalizing() no longer needs to be a special case in gcmodule.c.

Yes, I'll send a message to python-dev.


Python tracker <report at bugs.python.org>

More information about the Python-bugs-list mailing list