[issue31254] WeakKeyDictionary/Mapping doesn't call __missing__

Raymond Hettinger report at bugs.python.org
Thu Aug 24 23:52:28 EDT 2017


Raymond Hettinger added the comment:

One way to do it:
-----------------

diff --git a/Lib/weakref.py b/Lib/weakref.py
index 1802f32a20..18f26ea8b2 100644
--- a/Lib/weakref.py
+++ b/Lib/weakref.py
@@ -136,6 +136,8 @@ class WeakValueDictionary(collections.abc.MutableMapping):
             self._commit_removals()
         o = self.data[key]()
         if o is None:
+            if hasattr(self, '__missing__'):
+                return self.__missing__(key)
             raise KeyError(key)
         else:
             return o

Another way to do it:
---------------------

diff --git a/Lib/weakref.py b/Lib/weakref.py
index 1802f32a20..9951b0fb06 100644
--- a/Lib/weakref.py
+++ b/Lib/weakref.py
@@ -131,12 +131,15 @@ class WeakValueDictionary(collections.abc.MutableMapping):
             key = l.pop()
             _remove_dead_weakref(d, key)

+    def __missing__(self, key):
+            raise KeyError(key)
+
     def __getitem__(self, key):
         if self._pending_removals:
             self._commit_removals()
         o = self.data[key]()
         if o is None:
-            raise KeyError(key)
+            return self.__missing__(key)
         else:
             return o

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue31254>
_______________________________________


More information about the Python-bugs-list mailing list