[Python-Dev] reference leaks, del, and annotations (original) (raw)

Jim Jewett jimjjewett at gmail.com
Fri Mar 31 03:16:35 CEST 2006


(Apologies for the thinko -- corrected because it was in the example code.)

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.__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()

Lightweight del methods (such as most resource managers) could set the cycle attribute to True, and thereby ensure that they won'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 today.

-jJ



More information about the Python-Dev mailing list