2 atexit.py - allow programmer to define multiple exit functions to be executed
3 upon normal program termination.
5 One public function, register, is defined.
12 """run any registered exit functions
14 _exithandlers is traversed in reverse order so functions are executed
19 func
, targs
, kargs
= _exithandlers
.pop()
22 def register(func
, *targs
, **kargs
):
23 """register a function to be executed upon normal program termination
25 func - function to be called at exit
26 targs - optional arguments to pass to func
27 kargs - optional keyword arguments to pass to func
29 _exithandlers
.append((func
, targs
, kargs
))
32 if hasattr(sys
, "exitfunc"):
33 # Assume it's another registered exit function - append it to our list
34 register(sys
.exitfunc
)
35 sys
.exitfunc
= _run_exitfuncs
39 if __name__
== "__main__":
43 print "running x2(%s)" % `n`
45 print "running x3(%s, kwd=%s)" % (`n`
, `kwd`
)
49 register(x3
, 5, "bar")
50 register(x3
, "no kwd args")