# Bug in recent versions of pyrex (probably 0.9.8.6, definitely 0.9.9) # The 'just_trap' code leaves the exception state set (So PyErr_Occurred # returns True), while 'trap_and_catch' resets the state properly. # # What happens is the "obj == 2" line triggers a PyObject_Cmp() which seems to # internally have a PyErr_Occurred check, which then raises the KeyError # exception that was left unset. x = {} def just_trap(obj): try: return x[1234] except KeyError: if obj == 2: return True return False def trap_and_catch(obj): try: return x[1234] except KeyError, e: if obj == 2: return True return False def test(): try: just_trap(1) except KeyError, e: print 'just_trap raised a key error, but shouldn\'t have :(' else: print 'hmm... just_trap seems to be fixed' try: trap_and_catch(1) except KeyError, e: print 'trap_and_catch raised a key error, but shouldn\'t have :(' else: print 'trap_and_catch worked as expected'