| 1 | n/a | """ |
|---|
| 2 | n/a | atexit.py - allow programmer to define multiple exit functions to be executed |
|---|
| 3 | n/a | upon normal program termination. |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | One public function, register, is defined. |
|---|
| 6 | 2 | """ |
|---|
| 7 | n/a | |
|---|
| 8 | 2 | __all__ = ["register"] |
|---|
| 9 | n/a | |
|---|
| 10 | 2 | import sys |
|---|
| 11 | n/a | |
|---|
| 12 | 2 | _exithandlers = [] |
|---|
| 13 | 2 | def _run_exitfuncs(): |
|---|
| 14 | n/a | """run any registered exit functions |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | _exithandlers is traversed in reverse order so functions are executed |
|---|
| 17 | n/a | last in, first out. |
|---|
| 18 | n/a | """ |
|---|
| 19 | n/a | |
|---|
| 20 | 5 | exc_info = None |
|---|
| 21 | 16 | while _exithandlers: |
|---|
| 22 | 11 | func, targs, kargs = _exithandlers.pop() |
|---|
| 23 | 11 | try: |
|---|
| 24 | 11 | func(*targs, **kargs) |
|---|
| 25 | 3 | except SystemExit: |
|---|
| 26 | 0 | exc_info = sys.exc_info() |
|---|
| 27 | 3 | except: |
|---|
| 28 | 3 | import traceback |
|---|
| 29 | 3 | print >> sys.stderr, "Error in atexit._run_exitfuncs:" |
|---|
| 30 | 3 | traceback.print_exc() |
|---|
| 31 | 3 | exc_info = sys.exc_info() |
|---|
| 32 | n/a | |
|---|
| 33 | 5 | if exc_info is not None: |
|---|
| 34 | 2 | raise exc_info[0], exc_info[1], exc_info[2] |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | |
|---|
| 37 | 2 | def register(func, *targs, **kargs): |
|---|
| 38 | n/a | """register a function to be executed upon normal program termination |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | func - function to be called at exit |
|---|
| 41 | n/a | targs - optional arguments to pass to func |
|---|
| 42 | n/a | kargs - optional keyword arguments to pass to func |
|---|
| 43 | n/a | |
|---|
| 44 | n/a | func is returned to facilitate usage as a decorator. |
|---|
| 45 | n/a | """ |
|---|
| 46 | 13 | _exithandlers.append((func, targs, kargs)) |
|---|
| 47 | 13 | return func |
|---|
| 48 | n/a | |
|---|
| 49 | 2 | if hasattr(sys, "exitfunc"): |
|---|
| 50 | n/a | # Assume it's another registered exit function - append it to our list |
|---|
| 51 | 1 | register(sys.exitfunc) |
|---|
| 52 | 2 | sys.exitfunc = _run_exitfuncs |
|---|
| 53 | n/a | |
|---|
| 54 | 2 | if __name__ == "__main__": |
|---|
| 55 | 0 | def x1(): |
|---|
| 56 | 0 | print "running x1" |
|---|
| 57 | 0 | def x2(n): |
|---|
| 58 | 0 | print "running x2(%r)" % (n,) |
|---|
| 59 | 0 | def x3(n, kwd=None): |
|---|
| 60 | 0 | print "running x3(%r, kwd=%r)" % (n, kwd) |
|---|
| 61 | n/a | |
|---|
| 62 | 0 | register(x1) |
|---|
| 63 | 0 | register(x2, 12) |
|---|
| 64 | 0 | register(x3, 5, "bar") |
|---|
| 65 | 0 | register(x3, "no kwd args") |
|---|