| Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 1 | # Python test set -- part 3, built-in operations. | 
 | 2 |  | 
 | 3 |  | 
| Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 4 | print('3. Operations') | 
 | 5 | print('XXX Mostly not yet implemented') | 
| Fred Drake | 762c1cb | 2000-08-31 19:48:52 +0000 | [diff] [blame] | 6 |  | 
 | 7 |  | 
| Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +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 |  | 
| Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 15 |     def __eq__(self, other): | 
| Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 16 |         if isinstance(other, self.__class__): | 
| Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +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 | 
| Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 25 | for stmt in ['d[x2] = 2', | 
 | 26 |              'z = d[x2]', | 
 | 27 |              'x2 in d', | 
| Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 28 |              'd.get(x2)', | 
 | 29 |              'd.setdefault(x2, 42)', | 
 | 30 |              'd.pop(x2)', | 
 | 31 |              'd.update({x2: 2})']: | 
 | 32 |     try: | 
| Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 33 |         exec(stmt) | 
| Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 34 |     except RuntimeError: | 
| Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 35 |         print("%s: caught the RuntimeError outside" % (stmt,)) | 
| Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 36 |     else: | 
| Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 37 |         print("%s: No exception passed through!" % (stmt,)) # old CPython behavior | 
| Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 38 |  | 
| Tim Peters | 0c6010b | 2001-05-23 23:33:57 +0000 | [diff] [blame] | 39 |  | 
 | 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 |  | 
 | 47 | d = {} | 
 | 48 | for i in range(5): | 
 | 49 |     d[i] = i | 
 | 50 | for i in range(5): | 
 | 51 |     del d[i] | 
 | 52 | for i in range(5, 9):  # i==8 was the problem | 
 | 53 |     d[i] = i | 
| Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 54 |  | 
 | 55 |  | 
 | 56 | # Another dict resizing bug (SF bug #1456209). | 
 | 57 | # This caused Segmentation faults or Illegal instructions. | 
 | 58 |  | 
 | 59 | class X(object): | 
 | 60 |     def __hash__(self): | 
 | 61 |         return 5 | 
 | 62 |     def __eq__(self, other): | 
 | 63 |         if resizing: | 
 | 64 |             d.clear() | 
 | 65 |         return False | 
 | 66 | d = {} | 
 | 67 | resizing = False | 
 | 68 | d[X()] = 1 | 
 | 69 | d[X()] = 2 | 
 | 70 | d[X()] = 3 | 
 | 71 | d[X()] = 4 | 
 | 72 | d[X()] = 5 | 
 | 73 | # now trigger a resize | 
 | 74 | resizing = True | 
 | 75 | d[9] = 6 | 
 | 76 |  | 
| Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 77 | print('resize bugs not triggered.') |