blob: 67e77aad278182964b0881b74caefb468a461a88 [file] [log] [blame]
Guido van Rossum3bead091992-01-27 17:00:37 +00001# Python test set -- part 3, built-in operations.
2
3
4print '3. Operations'
Fred Drake762c1cb2000-08-31 19:48:52 +00005print 'XXX Mostly not yet implemented'
6
7
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00008print '3.1 Dictionary lookups fail if __cmp__() raises an exception'
Fred Drake762c1cb2000-08-31 19:48:52 +00009
Fred Drake004d5e62000-10-23 17:22:08 +000010class BadDictKey:
Tim Peters9a828d32001-05-29 21:14:32 +000011
Fred Drake004d5e62000-10-23 17:22:08 +000012 def __hash__(self):
13 return hash(self.__class__)
Fred Drake762c1cb2000-08-31 19:48:52 +000014
Guido van Rossum47b9ff62006-08-24 00:41:19 +000015 def __eq__(self, other):
Fred Drake004d5e62000-10-23 17:22:08 +000016 if isinstance(other, self.__class__):
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000017 print "raising error"
Fred Drake004d5e62000-10-23 17:22:08 +000018 raise RuntimeError, "gotcha"
19 return other
Fred Drake762c1cb2000-08-31 19:48:52 +000020
21d = {}
22x1 = BadDictKey()
23x2 = BadDictKey()
24d[x1] = 1
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000025for stmt in ['d[x2] = 2',
26 'z = d[x2]',
27 'x2 in d',
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000028 'd.get(x2)',
29 'd.setdefault(x2, 42)',
30 'd.pop(x2)',
31 'd.update({x2: 2})']:
32 try:
33 exec stmt
34 except RuntimeError:
35 print "%s: caught the RuntimeError outside" % (stmt,)
36 else:
Guido van Rossum47b9ff62006-08-24 00:41:19 +000037 print "%s: No exception passed through!" % (stmt,) # old CPython behavior
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000038
Tim Peters0c6010b2001-05-23 23:33:57 +000039
40# Dict resizing bug, found by Jack Jansen in 2.2 CVS development.
41# This version got an assert failure in debug build, infinite loop in
42# release build. Unfortunately, provoking this kind of stuff requires
43# a mix of inserts and deletes hitting exactly the right hash codes in
44# exactly the right order, and I can't think of a randomized approach
45# that would be *likely* to hit a failing case in reasonable time.
46
47d = {}
48for i in range(5):
49 d[i] = i
50for i in range(5):
51 del d[i]
52for i in range(5, 9): # i==8 was the problem
53 d[i] = i
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000054
55
56# Another dict resizing bug (SF bug #1456209).
57# This caused Segmentation faults or Illegal instructions.
58
59class X(object):
60 def __hash__(self):
61 return 5
62 def __eq__(self, other):
63 if resizing:
64 d.clear()
65 return False
66d = {}
67resizing = False
68d[X()] = 1
69d[X()] = 2
70d[X()] = 3
71d[X()] = 4
72d[X()] = 5
73# now trigger a resize
74resizing = True
75d[9] = 6
76
77print 'resize bugs not triggered.'