Fred Drake | c19425d | 2000-06-28 15:07:31 +0000 | [diff] [blame] | 1 | """ |
| 2 | atexit.py - allow programmer to define multiple exit functions to be executed |
| 3 | upon normal program termination. |
| 4 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 5 | One public function, register, is defined. |
Fred Drake | c19425d | 2000-06-28 15:07:31 +0000 | [diff] [blame] | 6 | """ |
| 7 | |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 8 | __all__ = ["register"] |
| 9 | |
Fred Drake | c19425d | 2000-06-28 15:07:31 +0000 | [diff] [blame] | 10 | _exithandlers = [] |
| 11 | def _run_exitfuncs(): |
| 12 | """run any registered exit functions |
| 13 | |
| 14 | _exithandlers is traversed in reverse order so functions are executed |
| 15 | last in, first out. |
| 16 | """ |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 17 | |
Skip Montanaro | 599bd5e | 2004-11-04 04:31:30 +0000 | [diff] [blame] | 18 | exc_info = None |
Fred Drake | c19425d | 2000-06-28 15:07:31 +0000 | [diff] [blame] | 19 | while _exithandlers: |
Tim Peters | 384fd10 | 2001-01-21 03:40:37 +0000 | [diff] [blame] | 20 | func, targs, kargs = _exithandlers.pop() |
Skip Montanaro | 599bd5e | 2004-11-04 04:31:30 +0000 | [diff] [blame] | 21 | try: |
| 22 | func(*targs, **kargs) |
| 23 | except SystemExit: |
| 24 | exc_info = sys.exc_info() |
| 25 | except: |
| 26 | import sys, traceback |
| 27 | print >> sys.stderr, "Error in atexit._run_exitfuncs:" |
| 28 | traceback.print_exc() |
| 29 | exc_info = sys.exc_info() |
| 30 | |
| 31 | if exc_info is not None: |
| 32 | raise exc_info[0], exc_info[1], exc_info[2] |
| 33 | |
Fred Drake | c19425d | 2000-06-28 15:07:31 +0000 | [diff] [blame] | 34 | |
| 35 | def register(func, *targs, **kargs): |
| 36 | """register a function to be executed upon normal program termination |
| 37 | |
| 38 | func - function to be called at exit |
| 39 | targs - optional arguments to pass to func |
| 40 | kargs - optional keyword arguments to pass to func |
| 41 | """ |
| 42 | _exithandlers.append((func, targs, kargs)) |
| 43 | |
| 44 | import sys |
Tim Peters | 012b69c | 2002-07-16 19:30:59 +0000 | [diff] [blame] | 45 | if hasattr(sys, "exitfunc"): |
| 46 | # Assume it's another registered exit function - append it to our list |
| 47 | register(sys.exitfunc) |
| 48 | sys.exitfunc = _run_exitfuncs |
Fred Drake | c19425d | 2000-06-28 15:07:31 +0000 | [diff] [blame] | 49 | del sys |
| 50 | |
| 51 | if __name__ == "__main__": |
| 52 | def x1(): |
| 53 | print "running x1" |
| 54 | def x2(n): |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 55 | print "running x2(%r)" % (n,) |
Fred Drake | c19425d | 2000-06-28 15:07:31 +0000 | [diff] [blame] | 56 | def x3(n, kwd=None): |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 57 | print "running x3(%r, kwd=%r)" % (n, kwd) |
Fred Drake | c19425d | 2000-06-28 15:07:31 +0000 | [diff] [blame] | 58 | |
| 59 | register(x1) |
| 60 | register(x2, 12) |
| 61 | register(x3, 5, "bar") |
| 62 | register(x3, "no kwd args") |