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 <rep...@bugs.python.org> <http://bugs.python.org/issue31254> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com