0001 # Python test set -- part 3, built-in operations. 0002 0003 0004 print '3. Operations' 0005 print 'XXX Mostly not yet implemented' 0006 0007 0008 print '3.1 Dictionary lookups succeed even if __cmp__() raises an exception' 0009 0010 # SourceForge bug #112558: 0011 # http://sourceforge.net/bugs/?func=detailbug&bug_id=112558&group_id=5470 0012 0013 class BadDictKey: 0014 already_printed_raising_error = 0 0015 0016 def __hash__(self): 0017 return hash(self.__class__) 0018 0019 def __cmp__(self, other): 0020 if isinstance(other, self.__class__): 0021 if not BadDictKey.already_printed_raising_error: 0022 # How many times __cmp__ gets called depends on the hash 0023 # code and the internals of the dict implementation; we 0024 # know it will be called at least once, but that's it. 0025 # already_printed_raising_error makes sure the expected- 0026 # output file prints the msg at most once. 0027 BadDictKey.already_printed_raising_error = 1 0028 print "raising error" 0029 raise RuntimeError, "gotcha" 0030 return other 0031 0032 d = {} 0033 x1 = BadDictKey() 0034 x2 = BadDictKey() 0035 d[x1] = 1 0036 d[x2] = 2 0037 print "No exception passed through." 0038 0039 # Dict resizing bug, found by Jack Jansen in 2.2 CVS development. 0040 # This version got an assert failure in debug build, infinite loop in 0041 # release build. Unfortunately, provoking this kind of stuff requires 0042 # a mix of inserts and deletes hitting exactly the right hash codes in 0043 # exactly the right order, and I can't think of a randomized approach 0044 # that would be *likely* to hit a failing case in reasonable time. 0045 0046 d = {} 0047 for i in range(5): 0048 d[i] = i 0049 for i in range(5): 0050 del d[i] 0051 for i in range(5, 9): # i==8 was the problem 0052 d[i] = i 0053
Generated by PyXR 0.9.4