summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_dict.py
diff options
context:
space:
mode:
authorCollin Winter <collinw@gmail.com>2007-03-12 15:57:19 (GMT)
committerCollin Winter <collinw@gmail.com>2007-03-12 15:57:19 (GMT)
commitf567ca3e1ae851ea6e7418da3df47e034841a08d (patch)
tree62a59ed8a8fca012efe1a4b4f67031452fd3ec81 /Lib/test/test_dict.py
parent7b9c555520436b8bd1805202e7b0d1329f05b321 (diff)
downloadcpython-f567ca3e1ae851ea6e7418da3df47e034841a08d.zip
cpython-f567ca3e1ae851ea6e7418da3df47e034841a08d.tar.gz
cpython-f567ca3e1ae851ea6e7418da3df47e034841a08d.tar.bz2
Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Diffstat (limited to 'Lib/test/test_dict.py')
-rw-r--r--Lib/test/test_dict.py71
1 files changed, 71 insertions, 0 deletions
diff --git a/Lib/test/test_dict.py b/Lib/test/test_dict.py
index e99c46d..e15ac0b 100644
--- a/Lib/test/test_dict.py
+++ b/Lib/test/test_dict.py
@@ -461,6 +461,77 @@ class DictTest(unittest.TestCase):
self.assertEqual(e.args, ((1,),))
else:
self.fail("missing KeyError")
+
+ def test_bad_key(self):
+ # Dictionary lookups should fail if __cmp__() raises an exception.
+ class CustomException(Exception):
+ pass
+
+ class BadDictKey:
+ def __hash__(self):
+ return hash(self.__class__)
+
+ def __cmp__(self, other):
+ if isinstance(other, self.__class__):
+ raise CustomException
+ return other
+
+ d = {}
+ x1 = BadDictKey()
+ x2 = BadDictKey()
+ d[x1] = 1
+ for stmt in ['d[x2] = 2',
+ 'z = d[x2]',
+ 'x2 in d',
+ 'd.has_key(x2)',
+ 'd.get(x2)',
+ 'd.setdefault(x2, 42)',
+ 'd.pop(x2)',
+ 'd.update({x2: 2})']:
+ try:
+ exec stmt in locals()
+ except CustomException:
+ pass
+ else:
+ self.fail("Statement didn't raise exception")
+
+ def test_resize1(self):
+ # Dict resizing bug, found by Jack Jansen in 2.2 CVS development.
+ # This version got an assert failure in debug build, infinite loop in
+ # release build. Unfortunately, provoking this kind of stuff requires
+ # a mix of inserts and deletes hitting exactly the right hash codes in
+ # exactly the right order, and I can't think of a randomized approach
+ # that would be *likely* to hit a failing case in reasonable time.
+
+ d = {}
+ for i in range(5):
+ d[i] = i
+ for i in range(5):
+ del d[i]
+ for i in range(5, 9): # i==8 was the problem
+ d[i] = i
+
+ def test_resize2(self):
+ # Another dict resizing bug (SF bug #1456209).
+ # This caused Segmentation faults or Illegal instructions.
+
+ class X(object):
+ def __hash__(self):
+ return 5
+ def __eq__(self, other):
+ if resizing:
+ d.clear()
+ return False
+ d = {}
+ resizing = False
+ d[X()] = 1
+ d[X()] = 2
+ d[X()] = 3
+ d[X()] = 4
+ d[X()] = 5
+ # now trigger a resize
+ resizing = True
+ d[9] = 6
from test import mapping_tests