[Python-Dev] Re: More fun with Python shutdown

Phillip J. Eby pje at telecommunity.com
Tue Nov 11 18:33:34 EST 2003


At 03:41 PM 11/11/03 -0500, Tim Peters wrote:
>[Phillip J. Eby]
> > ...
> > Actually, the funny thing here is that it's unlikely that the cycle a
> > type is in involves its base classes.
>
>Well, all new-style classes are in cycles with bases:
>
> >>> class C(object): pass
>..
> >>> object.__subclasses__()[-1]  # so C is reachable from object
><class '__main__.C'>

I thought this was done with weak references.


> >>> C.__mro__                    # and object is reachable from C
>(<class '__main__.C'>, <type 'object'>)
> >>>
>
>For that matter, since the first element of the MRO is the class itself, a

Oops.  I forgot about that.



>A complication in all this is that Python's cyclic gc never calls tp_dealloc
>or tp_free directly!  The only cleanup slot it calls directly is tp_clear.
>Deallocations still occur only as side effects of refcounts falling to 0, as
>tp_clear actions break cycles (and execute Py_DECREFs along the way).
>
>This protects against a class's tp_dealloc (but not tp_clear) getting called
>while instances still exist, even if they're all in one cycle.  But "still
>exist" gets fuzzy then.

Hm.  So what if tp_clear didn't mess with the MRO, except to decref its 
self-reference in the MRO?  tp_dealloc would have to decref the MRO tuple 
then, and deal with the off-by-one refcount for the type that would result 
from the tuple's deallocation.  Could that work?




More information about the Python-Dev mailing list