]> granicus.if.org Git - python/commitdiff
Use weakrefs to hold onto classes #2521.
authorBenjamin Peterson <benjamin@python.org>
Sat, 21 Aug 2010 03:03:22 +0000 (03:03 +0000)
committerBenjamin Peterson <benjamin@python.org>
Sat, 21 Aug 2010 03:03:22 +0000 (03:03 +0000)
This also causes the _weakref module to be built into the core.

Lib/abc.py
Lib/test/test_abc.py
Misc/NEWS
Modules/Setup.dist

index 515ba08f23cadab56bf009953eb9f7ace3f6ebdb..02e48a1bb32f9bb0353180e4b0546e6985b0725d 100644 (file)
@@ -5,6 +5,7 @@
 
 import types
 
+from _weakrefset import WeakSet
 
 # Instance of old-style class
 class _C: pass
@@ -95,9 +96,9 @@ class ABCMeta(type):
                     abstracts.add(name)
         cls.__abstractmethods__ = frozenset(abstracts)
         # Set up inheritance registry
-        cls._abc_registry = set()
-        cls._abc_cache = set()
-        cls._abc_negative_cache = set()
+        cls._abc_registry = WeakSet()
+        cls._abc_cache = WeakSet()
+        cls._abc_negative_cache = WeakSet()
         cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter
         return cls
 
@@ -128,7 +129,7 @@ class ABCMeta(type):
         """Override for isinstance(instance, cls)."""
         # Inline the cache checking when it's simple.
         subclass = getattr(instance, '__class__', None)
-        if subclass in cls._abc_cache:
+        if subclass is not None and subclass in cls._abc_cache:
             return True
         subtype = type(instance)
         # Old-style instances
@@ -152,7 +153,7 @@ class ABCMeta(type):
         # Check negative cache; may have to invalidate
         if cls._abc_negative_cache_version < ABCMeta._abc_invalidation_counter:
             # Invalidate the negative cache
-            cls._abc_negative_cache = set()
+            cls._abc_negative_cache = WeakSet()
             cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter
         elif subclass in cls._abc_negative_cache:
             return False
index b5af46b522ff317b5682d4683be6f7b559801c6d..6a8c3a132742720c0bb1aef35ecbecb79f30f18d 100644 (file)
@@ -3,7 +3,7 @@
 
 """Unit tests for abc.py."""
 
-import unittest
+import unittest, weakref
 from test import test_support
 
 import abc
@@ -208,6 +208,22 @@ class TestABC(unittest.TestCase):
         C()
         self.assertEqual(B.counter, 1)
 
+    def test_cache_leak(self):
+        # See issue #2521.
+        class A(object):
+            __metaclass__ = abc.ABCMeta
+            @abc.abstractmethod
+            def f(self):
+                pass
+        class C(A):
+            def f(self):
+                A.f(self)
+        r = weakref.ref(C)
+        # Trigger cache.
+        C().f()
+        del C
+        test_support.gc_collect()
+        self.assertEqual(r(), None)
 
 def test_main():
     test_support.run_unittest(TestABC)
index 29bc2767e35807ae00b973455f7d7ff93dd2eeb6..349cc7f2d733df0be53036d4ff1a4e3fa5a2de49 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -31,6 +31,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #2521: Use weakrefs on for caching in the abc module, so that classes
+  are not held onto after they are deleted elsewhere.
+
 - Issue #9626: the view methods for collections.OrderedDict() were returning
   the unordered versions inherited from dict.  Those methods are now
   overridden to provide ordered views.
@@ -188,6 +191,9 @@ Library
 Extension Modules
 -----------------
 
+- As a result of issue #2521, the _weakref module is now compiled into the
+  interpreter by default.
+
 - Issue #9324: Add parameter validation to signal.signal on Windows in order
   to prevent crashes.
 
index e76e0dfe8ffb22f66de7977c21dd60092d74481e..e2b0c59fee3122af320369972c1a6679e9b31249 100644 (file)
@@ -118,6 +118,7 @@ pwd pwdmodule.c                     # this is needed to find out the user's home dir
                                # if $HOME is not set
 _sre _sre.c                    # Fredrik Lundh's new regular expressions
 _codecs _codecsmodule.c                # access to the builtin codecs and codec registry
+_weakref _weakref.c             # weak references
 
 # The zipimport module is always imported at startup. Having it as a
 # builtin module avoids some bootstrapping problems and reduces overhead.