atexit.py - allow programmer to define multiple exit functions to be executed
upon normal program termination.
One public function, register, is defined.
"""run any registered exit functions
_exithandlers is traversed in reverse order so functions are executed
func, targs, kargs = _exithandlers.pop()
exc_info = sys.exc_info()
print >> sys.stderr, "Error in atexit._run_exitfuncs:"
exc_info = sys.exc_info()
raise exc_info[0], exc_info[1], exc_info[2]
def register(func, *targs, **kargs):
"""register a function to be executed upon normal program termination
func - function to be called at exit
targs - optional arguments to pass to func
kargs - optional keyword arguments to pass to func
func is returned to facilitate usage as a decorator.
_exithandlers.append((func, targs, kargs))
if hasattr(sys, "exitfunc"):
# Assume it's another registered exit function - append it to our list
sys.exitfunc = _run_exitfuncs
if __name__ == "__main__":
print "running x2(%r)" % (n,)
print "running x3(%r, kwd=%r)" % (n, kwd)
register(x3, "no kwd args")