blob: fafc0622119d8b8537d264474598abfdf3b55405 [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
Armin Rigo35f6d362006-06-01 13:19:12 +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
Fred Drake004d5e62000-10-23 17:22:08 +000015 def __cmp__(self, other):
16 if isinstance(other, self.__class__):
Armin Rigo35f6d362006-06-01 13:19:12 +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
Armin Rigo35f6d362006-06-01 13:19:12 +000025for stmt in ['d[x2] = 2',
26 'z = d[x2]',
27 'x2 in d',
28 'd.has_key(x2)',
29 'd.get(x2)',
30 'd.setdefault(x2, 42)',
31 'd.pop(x2)',
32 'd.update({x2: 2})']:
33 try:
34 exec stmt
35 except RuntimeError:
36 print "%s: caught the RuntimeError outside" % (stmt,)
37 else:
38 print "%s: No exception passed through!" # old CPython behavior
39
Tim Peters0c6010b2001-05-23 23:33:57 +000040
41# Dict resizing bug, found by Jack Jansen in 2.2 CVS development.
42# This version got an assert failure in debug build, infinite loop in
43# release build. Unfortunately, provoking this kind of stuff requires
44# a mix of inserts and deletes hitting exactly the right hash codes in
45# exactly the right order, and I can't think of a randomized approach
46# that would be *likely* to hit a failing case in reasonable time.
47
48d = {}
49for i in range(5):
50 d[i] = i
51for i in range(5):
52 del d[i]
53for i in range(5, 9): # i==8 was the problem
54 d[i] = i
Armin Rigo35f6d362006-06-01 13:19:12 +000055
56
57# Another dict resizing bug (SF bug #1456209).
58# This caused Segmentation faults or Illegal instructions.
59
60class X(object):
61 def __hash__(self):
62 return 5
63 def __eq__(self, other):
64 if resizing:
65 d.clear()
66 return False
67d = {}
68resizing = False
69d[X()] = 1
70d[X()] = 2
71d[X()] = 3
72d[X()] = 4
73d[X()] = 5
74# now trigger a resize
75resizing = True
76d[9] = 6
77
78print 'resize bugs not triggered.'