This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author rhettinger
Recipients Antony.Lee, pitrou, rhettinger
Date 2017-08-25.03:52:27
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1503633148.16.0.332850574969.issue31254@psf.upfronthosting.co.za>
In-reply-to
Content
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
History
Date User Action Args
2017-08-25 03:52:28rhettingersetrecipients: + rhettinger, pitrou, Antony.Lee
2017-08-25 03:52:28rhettingersetmessageid: <1503633148.16.0.332850574969.issue31254@psf.upfronthosting.co.za>
2017-08-25 03:52:28rhettingerlinkissue31254 messages
2017-08-25 03:52:27rhettingercreate