[Python-Dev] reference leaks, __del__, and annotations

Greg Ewing greg.ewing at canterbury.ac.nz
Fri Mar 31 04:21:13 CEST 2006

Jim Jewett wrote:

> The checkins list has been struggling with generator reference leaks;
> the latest conclusion was that some are unavoidable because of __del__
> cycles.  That sort of defeats the purpose of resource managers.

Seems to me we need a whole new approach to
finalization that's friendly to cyclic gc,
such as a way of registering a finalizer that
doesn't depend on the original object.

If such a mechanism were available, could it
be used instead of a __del__ method to clean
up after a generator? (I'm asking because I'm
not sure exactly what a generator __del__ needs
to do.)

> As a strawman proposal:
>     deletes = [(obj.__del__.cycle, obj) for obj in cycle
>                           if hasattr(obj, "__del__") and
> hasattr(obj.__del__, "cycle")]
>     deletes.sort()
>     for (cycle, obj) in deletes:
>         obj.__del__()

I think we need to be very careful about doing
anything like this. From what Tim said recently,
the consequences of an object getting its __del__
annotation wrong could be as bad as crashing the


More information about the Python-Dev mailing list