python-2.5.2/win32/Lib/test/test_operations.py
changeset 0 ae805ac0140d
equal deleted inserted replaced
-1:000000000000 0:ae805ac0140d
       
     1 # Python test set -- part 3, built-in operations.
       
     2 
       
     3 
       
     4 print '3. Operations'
       
     5 print 'XXX Mostly not yet implemented'
       
     6 
       
     7 
       
     8 print '3.1 Dictionary lookups fail if __cmp__() raises an exception'
       
     9 
       
    10 class 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 
       
    21 d = {}
       
    22 x1 = BadDictKey()
       
    23 x2 = BadDictKey()
       
    24 d[x1] = 1
       
    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 
       
    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
       
    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.'