blob: c9f4cc677528a7822f71860f34c16d1ceeb10974 [file] [log] [blame]
Fred Drakec19425d2000-06-28 15:07:31 +00001"""
2atexit.py - allow programmer to define multiple exit functions to be executed
3upon normal program termination.
4
Tim Peters146965a2001-01-14 18:09:23 +00005One public function, register, is defined.
Fred Drakec19425d2000-06-28 15:07:31 +00006"""
7
Skip Montanaroe99d5ea2001-01-20 19:54:20 +00008__all__ = ["register"]
9
Raymond Hettingera9ef5e52004-12-11 02:49:40 +000010import sys
11
Fred Drakec19425d2000-06-28 15:07:31 +000012_exithandlers = []
13def _run_exitfuncs():
14 """run any registered exit functions
15
16 _exithandlers is traversed in reverse order so functions are executed
17 last in, first out.
18 """
Tim Peters146965a2001-01-14 18:09:23 +000019
Skip Montanaro599bd5e2004-11-04 04:31:30 +000020 exc_info = None
Fred Drakec19425d2000-06-28 15:07:31 +000021 while _exithandlers:
Tim Peters384fd102001-01-21 03:40:37 +000022 func, targs, kargs = _exithandlers.pop()
Skip Montanaro599bd5e2004-11-04 04:31:30 +000023 try:
24 func(*targs, **kargs)
25 except SystemExit:
26 exc_info = sys.exc_info()
27 except:
Raymond Hettingera9ef5e52004-12-11 02:49:40 +000028 import traceback
Skip Montanaro599bd5e2004-11-04 04:31:30 +000029 print >> sys.stderr, "Error in atexit._run_exitfuncs:"
30 traceback.print_exc()
31 exc_info = sys.exc_info()
32
33 if exc_info is not None:
34 raise exc_info[0], exc_info[1], exc_info[2]
35
Fred Drakec19425d2000-06-28 15:07:31 +000036
37def register(func, *targs, **kargs):
38 """register a function to be executed upon normal program termination
39
40 func - function to be called at exit
41 targs - optional arguments to pass to func
42 kargs - optional keyword arguments to pass to func
43 """
44 _exithandlers.append((func, targs, kargs))
45
Tim Peters012b69c2002-07-16 19:30:59 +000046if hasattr(sys, "exitfunc"):
47 # Assume it's another registered exit function - append it to our list
48 register(sys.exitfunc)
49sys.exitfunc = _run_exitfuncs
Fred Drakec19425d2000-06-28 15:07:31 +000050
51if __name__ == "__main__":
52 def x1():
53 print "running x1"
54 def x2(n):
Walter Dörwald70a6b492004-02-12 17:35:32 +000055 print "running x2(%r)" % (n,)
Fred Drakec19425d2000-06-28 15:07:31 +000056 def x3(n, kwd=None):
Walter Dörwald70a6b492004-02-12 17:35:32 +000057 print "running x3(%r, kwd=%r)" % (n, kwd)
Fred Drakec19425d2000-06-28 15:07:31 +000058
59 register(x1)
60 register(x2, 12)
61 register(x3, 5, "bar")
62 register(x3, "no kwd args")