Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 1 | # Python test set -- part 3, built-in operations. |
| 2 | |
| 3 | |
| 4 | print '3. Operations' |
Fred Drake | 762c1cb | 2000-08-31 19:48:52 +0000 | [diff] [blame] | 5 | print 'XXX Mostly not yet implemented' |
| 6 | |
| 7 | |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 8 | print '3.1 Dictionary lookups fail if __cmp__() raises an exception' |
Fred Drake | 762c1cb | 2000-08-31 19:48:52 +0000 | [diff] [blame] | 9 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 10 | class BadDictKey: |
Tim Peters | 9a828d3 | 2001-05-29 21:14:32 +0000 | [diff] [blame] | 11 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 12 | def __hash__(self): |
| 13 | return hash(self.__class__) |
Fred Drake | 762c1cb | 2000-08-31 19:48:52 +0000 | [diff] [blame] | 14 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 15 | def __cmp__(self, other): |
| 16 | if isinstance(other, self.__class__): |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 17 | print "raising error" |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 18 | raise RuntimeError, "gotcha" |
| 19 | return other |
Fred Drake | 762c1cb | 2000-08-31 19:48:52 +0000 | [diff] [blame] | 20 | |
| 21 | d = {} |
| 22 | x1 = BadDictKey() |
| 23 | x2 = BadDictKey() |
| 24 | d[x1] = 1 |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 25 | for 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 Peters | 0c6010b | 2001-05-23 23:33:57 +0000 | [diff] [blame] | 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 | |
| 48 | d = {} |
| 49 | for i in range(5): |
| 50 | d[i] = i |
| 51 | for i in range(5): |
| 52 | del d[i] |
| 53 | for i in range(5, 9): # i==8 was the problem |
| 54 | d[i] = i |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 55 | |
| 56 | |
| 57 | # Another dict resizing bug (SF bug #1456209). |
| 58 | # This caused Segmentation faults or Illegal instructions. |
| 59 | |
| 60 | class X(object): |
| 61 | def __hash__(self): |
| 62 | return 5 |
| 63 | def __eq__(self, other): |
| 64 | if resizing: |
| 65 | d.clear() |
| 66 | return False |
| 67 | d = {} |
| 68 | resizing = False |
| 69 | d[X()] = 1 |
| 70 | d[X()] = 2 |
| 71 | d[X()] = 3 |
| 72 | d[X()] = 4 |
| 73 | d[X()] = 5 |
| 74 | # now trigger a resize |
| 75 | resizing = True |
| 76 | d[9] = 6 |
| 77 | |
| 78 | print 'resize bugs not triggered.' |