[Python-Dev] reference leaks, __del__, and annotations
jimjjewett at gmail.com
Fri Mar 31 03:07:46 CEST 2006
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. (Yes,
it can be worked around on a case-by-case basis.)
As I see it, part of the problem is that
(1) When there is a cycle, python refuses to guess.
(2) There is no way for a __del__ method to hint at ordering constraints.
(3) There is no lightweight version of __del__ to say "I don't care
about ordering constraints."
How about using an (optional) annotation on __del__ methods, to
indicate how cycles should be broken?
As a strawman proposal:
deletes = [obj for obj in cycle if hasattr(obj, "cycle")]
for obj in deletes:
Lightweight __del__ methods (such as most resource managers) could set
the cycle attribute to True, and thereby ensure that they don't cause
unbreakable cycles. Fancier object frameworks could use different
values for the cycle attribute. Any object whose __del__ is not
annotated will still be at least as likely to get finalized as it is
More information about the Python-Dev