blob: 51a153e7e918e1e784e48abf6fd32d113cc7764f [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
Guido van Rossumbe19ed72007-02-09 05:37:30 +000029 print("Error in atexit._run_exitfuncs:", file=sys.stderr)
Skip Montanaro599bd5e2004-11-04 04:31:30 +000030 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
Thomas Wouters89f507f2006-12-13 04:49:30 +000043
44 func is returned to facilitate usage as a decorator.
Fred Drakec19425d2000-06-28 15:07:31 +000045 """
46 _exithandlers.append((func, targs, kargs))
Thomas Wouters89f507f2006-12-13 04:49:30 +000047 return func
Fred Drakec19425d2000-06-28 15:07:31 +000048
Tim Peters012b69c2002-07-16 19:30:59 +000049if hasattr(sys, "exitfunc"):
50 # Assume it's another registered exit function - append it to our list
51 register(sys.exitfunc)
52sys.exitfunc = _run_exitfuncs
Fred Drakec19425d2000-06-28 15:07:31 +000053
54if __name__ == "__main__":
55 def x1():
Guido van Rossumbe19ed72007-02-09 05:37:30 +000056 print("running x1")
Fred Drakec19425d2000-06-28 15:07:31 +000057 def x2(n):
Guido van Rossumbe19ed72007-02-09 05:37:30 +000058 print("running x2(%r)" % (n,))
Fred Drakec19425d2000-06-28 15:07:31 +000059 def x3(n, kwd=None):
Guido van Rossumbe19ed72007-02-09 05:37:30 +000060 print("running x3(%r, kwd=%r)" % (n, kwd))
Fred Drakec19425d2000-06-28 15:07:31 +000061
62 register(x1)
63 register(x2, 12)
64 register(x3, 5, "bar")
65 register(x3, "no kwd args")