I’ve been reading some of the code in a standard threading library ( Python 2.6 ) and there was a piece of code which made me wonder. It can be shorten to the following structure ( compare to __bootstrap_inner method in threading.py ):
def foo():
exc_type, exc_value, exc_tb = sys.exc_info()
try:
# some code
except:
# some code
finally:
del exc_type, exc_value, exc_tb
These variables do not go outside of foo scope. Is there any reason to delete these references at the end?
Yes, at the very least for
exc_tb; traceback objects hold a reference to the current frame, and that makes this a circular reference.By deleting the local reference you break that circle, so you don’t have to hope and trust that the garbage collector will be able to.
From the
sys.exc_info()function docs: