From 4fd06e0170aa75e4873b73f733bfd3f3de19d967 Mon Sep 17 00:00:00 2001 From: Fred Drake Date: Fri, 3 Aug 2001 04:11:27 +0000 Subject: [PATCH] Make sure that WeakValueDictionary[] raises KeyError instead of TypeError for keys that are not in the dictionary. --- Lib/test/test_weakref.py | 5 +++++ Lib/weakref.py | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_weakref.py b/Lib/test/test_weakref.py index bb4ce7639ef..341d53fae28 100644 --- a/Lib/test/test_weakref.py +++ b/Lib/test/test_weakref.py @@ -252,6 +252,11 @@ class MappingTestCase(TestBase): del objects, o self.assert_(len(dict) == 0, "deleting the values did not clear the dictionary") + # regression on SF bug #447152: + dict = weakref.WeakValueDictionary() + self.assertRaises(KeyError, dict.__getitem__, 1) + dict[2] = C() + self.assertRaises(KeyError, dict.__getitem__, 2) def test_weak_keys(self): # diff --git a/Lib/weakref.py b/Lib/weakref.py index cf950baef72..1d21e798867 100644 --- a/Lib/weakref.py +++ b/Lib/weakref.py @@ -41,7 +41,7 @@ class WeakValueDictionary(UserDict.UserDict): # way in). def __getitem__(self, key): - o = self.data.get(key)() + o = self.data[key]() if o is None: raise KeyError, key else: