Commit a9ef5e56 authored by Raymond Hettinger's avatar Raymond Hettinger

SF bug #1083202L UnboundLocalError raised by atexit module

The sys module could be called before being imported.
parent bb4f1bdd
...@@ -7,6 +7,8 @@ One public function, register, is defined. ...@@ -7,6 +7,8 @@ One public function, register, is defined.
__all__ = ["register"] __all__ = ["register"]
import sys
_exithandlers = [] _exithandlers = []
def _run_exitfuncs(): def _run_exitfuncs():
"""run any registered exit functions """run any registered exit functions
...@@ -23,7 +25,7 @@ def _run_exitfuncs(): ...@@ -23,7 +25,7 @@ def _run_exitfuncs():
except SystemExit: except SystemExit:
exc_info = sys.exc_info() exc_info = sys.exc_info()
except: except:
import sys, traceback import traceback
print >> sys.stderr, "Error in atexit._run_exitfuncs:" print >> sys.stderr, "Error in atexit._run_exitfuncs:"
traceback.print_exc() traceback.print_exc()
exc_info = sys.exc_info() exc_info = sys.exc_info()
...@@ -41,12 +43,10 @@ def register(func, *targs, **kargs): ...@@ -41,12 +43,10 @@ def register(func, *targs, **kargs):
""" """
_exithandlers.append((func, targs, kargs)) _exithandlers.append((func, targs, kargs))
import sys
if hasattr(sys, "exitfunc"): if hasattr(sys, "exitfunc"):
# Assume it's another registered exit function - append it to our list # Assume it's another registered exit function - append it to our list
register(sys.exitfunc) register(sys.exitfunc)
sys.exitfunc = _run_exitfuncs sys.exitfunc = _run_exitfuncs
del sys
if __name__ == "__main__": if __name__ == "__main__":
def x1(): def x1():
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment