source: trunk/essentials/dev-lang/python/Lib/test/test_operations.py@ 3951

Last change on this file since 3951 was 3225, checked in by bird, 19 years ago

Python 2.5

File size: 1.8 KB
Line 
1# Python test set -- part 3, built-in operations.
2
3
4print '3. Operations'
5print 'XXX Mostly not yet implemented'
6
7
8print '3.1 Dictionary lookups fail if __cmp__() raises an exception'
9
10class BadDictKey:
11
12 def __hash__(self):
13 return hash(self.__class__)
14
15 def __cmp__(self, other):
16 if isinstance(other, self.__class__):
17 print "raising error"
18 raise RuntimeError, "gotcha"
19 return other
20
21d = {}
22x1 = BadDictKey()
23x2 = BadDictKey()
24d[x1] = 1
25for 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
40
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
55
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.'
Note: See TracBrowser for help on using the repository browser.