| 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() |
| 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 |
| _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(%s)" % `n` |
| print "running x3(%s, kwd=%s)" % (`n`, `kwd`) |
| register(x3, "no kwd args") |