summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_dict.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_dict.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_dict.py')
-rw-r--r--Lib/test/test_dict.py50
1 files changed, 50 insertions, 0 deletions
diff --git a/Lib/test/test_dict.py b/Lib/test/test_dict.py
index e13829c..f3f78e7 100644
--- a/Lib/test/test_dict.py
+++ b/Lib/test/test_dict.py
@@ -395,6 +395,56 @@ class DictTest(unittest.TestCase):
else:
self.fail("< didn't raise Exc")
+ def test_missing(self):
+ # Make sure dict doesn't have a __missing__ method
+ self.assertEqual(hasattr(dict, "__missing__"), False)
+ self.assertEqual(hasattr({}, "__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(dict):
+ 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(dict):
+ 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(dict):
+ def __init__(self):
+ # An instance variable __missing__ should have no effect
+ self.__missing__ = lambda key: None
+ f = F()
+ try:
+ f[42]
+ except KeyError, err:
+ self.assertEqual(err.args, (42,))
+ else:
+ self.fail_("f[42] didn't raise KeyError")
+ class G(dict):
+ pass
+ g = G()
+ try:
+ g[42]
+ except KeyError, err:
+ self.assertEqual(err.args, (42,))
+ else:
+ self.fail_("g[42] didn't raise KeyError")
+
+
import mapping_tests
class GeneralMappingTests(mapping_tests.BasicTestMappingProtocol):