summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_userdict.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2006-02-25 22:38:04 (GMT)
committerGuido van Rossum <guido@python.org>2006-02-25 22:38:04 (GMT)
commit1968ad32cd7f46d9bb64826672ef68cdaee35288 (patch)
treec46db5a446d9de18fb8436408ec29d2111a2f5ad /Lib/test/test_userdict.py
parentab51f5f24d6f6edef5e8fac5e31b2e4ac0cbdbac (diff)
downloadcpython-1968ad32cd7f46d9bb64826672ef68cdaee35288.zip
cpython-1968ad32cd7f46d9bb64826672ef68cdaee35288.tar.gz
cpython-1968ad32cd7f46d9bb64826672ef68cdaee35288.tar.bz2
- Patch 1433928:
- The copy module now "copies" function objects (as atomic objects). - dict.__getitem__ now looks for a __missing__ hook before raising KeyError. - Added a new type, defaultdict, to the collections module. This uses the new __missing__ hook behavior added to dict (see above).
Diffstat (limited to 'Lib/test/test_userdict.py')
-rw-r--r--Lib/test/test_userdict.py49
1 files changed, 49 insertions, 0 deletions
diff --git a/Lib/test/test_userdict.py b/Lib/test/test_userdict.py
index 2d5fa03..a4b7de4 100644
--- a/Lib/test/test_userdict.py
+++ b/Lib/test/test_userdict.py
@@ -148,6 +148,55 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol):
self.assertEqual(t.popitem(), ("x", 42))
self.assertRaises(KeyError, t.popitem)
+ def test_missing(self):
+ # Make sure UserDict doesn't have a __missing__ method
+ self.assertEqual(hasattr(UserDict, "__missing__"), False)
+ # Test several cases:
+ # (D) subclass defines __missing__ method returning a value
+ # (E) subclass defines __missing__ method raising RuntimeError
+ # (F) subclass sets __missing__ instance variable (no effect)
+ # (G) subclass doesn't define __missing__ at a all
+ class D(UserDict.UserDict):
+ def __missing__(self, key):
+ return 42
+ d = D({1: 2, 3: 4})
+ self.assertEqual(d[1], 2)
+ self.assertEqual(d[3], 4)
+ self.assert_(2 not in d)
+ self.assert_(2 not in d.keys())
+ self.assertEqual(d[2], 42)
+ class E(UserDict.UserDict):
+ def __missing__(self, key):
+ raise RuntimeError(key)
+ e = E()
+ try:
+ e[42]
+ except RuntimeError, err:
+ self.assertEqual(err.args, (42,))
+ else:
+ self.fail_("e[42] didn't raise RuntimeError")
+ class F(UserDict.UserDict):
+ def __init__(self):
+ # An instance variable __missing__ should have no effect
+ self.__missing__ = lambda key: None
+ UserDict.UserDict.__init__(self)
+ f = F()
+ try:
+ f[42]
+ except KeyError, err:
+ self.assertEqual(err.args, (42,))
+ else:
+ self.fail_("f[42] didn't raise KeyError")
+ class G(UserDict.UserDict):
+ pass
+ g = G()
+ try:
+ g[42]
+ except KeyError, err:
+ self.assertEqual(err.args, (42,))
+ else:
+ self.fail_("g[42] didn't raise KeyError")
+
##########################
# Test Dict Mixin