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 | |
| 8 | print '3.1 Dictionary lookups succeed even if __cmp__() raises an exception' |
| 9 | |
| 10 | # SourceForge bug #112558: |
| 11 | # http://sourceforge.net/bugs/?func=detailbug&bug_id=112558&group_id=5470 |
| 12 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 13 | class BadDictKey: |
Tim Peters | 9a828d3 | 2001-05-29 21:14:32 +0000 | [diff] [blame] | 14 | already_printed_raising_error = 0 |
| 15 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 16 | def __hash__(self): |
| 17 | return hash(self.__class__) |
Fred Drake | 762c1cb | 2000-08-31 19:48:52 +0000 | [diff] [blame] | 18 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 19 | def __cmp__(self, other): |
| 20 | if isinstance(other, self.__class__): |
Tim Peters | 9a828d3 | 2001-05-29 21:14:32 +0000 | [diff] [blame] | 21 | if not BadDictKey.already_printed_raising_error: |
| 22 | # How many times __cmp__ gets called depends on the hash |
| 23 | # code and the internals of the dict implementation; we |
| 24 | # know it will be called at least once, but that's it. |
| 25 | # already_printed_raising_error makes sure the expected- |
| 26 | # output file prints the msg at most once. |
| 27 | BadDictKey.already_printed_raising_error = 1 |
| 28 | print "raising error" |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 29 | raise RuntimeError, "gotcha" |
| 30 | return other |
Fred Drake | 762c1cb | 2000-08-31 19:48:52 +0000 | [diff] [blame] | 31 | |
| 32 | d = {} |
| 33 | x1 = BadDictKey() |
| 34 | x2 = BadDictKey() |
| 35 | d[x1] = 1 |
| 36 | d[x2] = 2 |
| 37 | print "No exception passed through." |
Tim Peters | 0c6010b | 2001-05-23 23:33:57 +0000 | [diff] [blame] | 38 | |
| 39 | # Dict resizing bug, found by Jack Jansen in 2.2 CVS development. |
| 40 | # This version got an assert failure in debug build, infinite loop in |
| 41 | # release build. Unfortunately, provoking this kind of stuff requires |
| 42 | # a mix of inserts and deletes hitting exactly the right hash codes in |
| 43 | # exactly the right order, and I can't think of a randomized approach |
| 44 | # that would be *likely* to hit a failing case in reasonable time. |
| 45 | |
| 46 | d = {} |
| 47 | for i in range(5): |
| 48 | d[i] = i |
| 49 | for i in range(5): |
| 50 | del d[i] |
| 51 | for i in range(5, 9): # i==8 was the problem |
| 52 | d[i] = i |