import sys class NoisyDestructor(object): def __del__(self): id(0) print("delete") class MyException(Exception): pass def error2(): o = NoisyDestructor() raise MyException def error(): # Create a file object which will be stored in a local variable. # Raising an exception creates a traceback object which has a # reference to the current frame which contains the local variable # which is our open file. f = open(__file__) o = NoisyDestructor() error2() sys.setrecursionlimit(10) try: try: print("error()") error() except Exception as exc: keep_ref = exc finally: sys.setrecursionlimit(100) print("exit")