ยปCore Development>Code coverage>Lib/atexit.py

Python code coverage for Lib/atexit.py

#countcontent
1n/a"""
2n/aatexit.py - allow programmer to define multiple exit functions to be executed
3n/aupon normal program termination.
4n/a
5n/aOne public function, register, is defined.
62"""
7n/a
82__all__ = ["register"]
9n/a
102import sys
11n/a
122_exithandlers = []
132def _run_exitfuncs():
14n/a """run any registered exit functions
15n/a
16n/a _exithandlers is traversed in reverse order so functions are executed
17n/a last in, first out.
18n/a """
19n/a
205 exc_info = None
2116 while _exithandlers:
2211 func, targs, kargs = _exithandlers.pop()
2311 try:
2411 func(*targs, **kargs)
253 except SystemExit:
260 exc_info = sys.exc_info()
273 except:
283 import traceback
293 print >> sys.stderr, "Error in atexit._run_exitfuncs:"
303 traceback.print_exc()
313 exc_info = sys.exc_info()
32n/a
335 if exc_info is not None:
342 raise exc_info[0], exc_info[1], exc_info[2]
35n/a
36n/a
372def register(func, *targs, **kargs):
38n/a """register a function to be executed upon normal program termination
39n/a
40n/a func - function to be called at exit
41n/a targs - optional arguments to pass to func
42n/a kargs - optional keyword arguments to pass to func
43n/a
44n/a func is returned to facilitate usage as a decorator.
45n/a """
4613 _exithandlers.append((func, targs, kargs))
4713 return func
48n/a
492if hasattr(sys, "exitfunc"):
50n/a # Assume it's another registered exit function - append it to our list
511 register(sys.exitfunc)
522sys.exitfunc = _run_exitfuncs
53n/a
542if __name__ == "__main__":
550 def x1():
560 print "running x1"
570 def x2(n):
580 print "running x2(%r)" % (n,)
590 def x3(n, kwd=None):
600 print "running x3(%r, kwd=%r)" % (n, kwd)
61n/a
620 register(x1)
630 register(x2, 12)
640 register(x3, 5, "bar")
650 register(x3, "no kwd args")