blob: 04f1d492df1584c79a6345bab0e2e3578dccfa40 [file] [log] [blame]
Fred Drakec19425d2000-06-28 15:07:31 +00001\section{\module{atexit} ---
Fred Drake3c62d9e2000-07-06 04:51:04 +00002 Exit handlers}
Fred Drakec19425d2000-06-28 15:07:31 +00003
Collin Winter670e6922007-03-21 02:57:17 +00004\declaremodule{builtin}{atexit}
Fred Drakec19425d2000-06-28 15:07:31 +00005\moduleauthor{Skip Montanaro}{skip@mojam.com}
6\sectionauthor{Skip Montanaro}{skip@mojam.com}
7\modulesynopsis{Register and execute cleanup functions.}
8
Fred Drake30f76ff2000-06-30 16:06:19 +00009\versionadded{2.0}
Fred Drakebe93a832000-06-28 22:07:55 +000010
Fred Drakec19425d2000-06-28 15:07:31 +000011
Collin Winter670e6922007-03-21 02:57:17 +000012The \module{atexit} module defines functions to register and
13unregister cleanup functions. Functions thus registered are
14automatically executed upon normal interpreter termination.
Fred Drakec19425d2000-06-28 15:07:31 +000015
Collin Winter670e6922007-03-21 02:57:17 +000016Note: the functions registered via this module are not called when
17the program is killed by a signal, when a Python fatal internal
18error is detected, or when \function{os._exit()} is called.
Skip Montanaro09151652000-07-05 23:11:26 +000019
Fred Drakec19425d2000-06-28 15:07:31 +000020\begin{funcdesc}{register}{func\optional{, *args\optional{, **kargs}}}
21Register \var{func} as a function to be executed at termination. Any
22optional arguments that are to be passed to \var{func} must be passed
23as arguments to \function{register()}.
24
25At normal program termination (for instance, if
26\function{sys.exit()} is called or the main module's execution
27completes), all functions registered are called in last in, first out
28order. The assumption is that lower level modules will normally be
29imported before higher level modules and thus must be cleaned up
30later.
Skip Montanaro599bd5e2004-11-04 04:31:30 +000031
Fred Drake99e5ce52004-11-04 05:45:44 +000032If an exception is raised during execution of the exit handlers, a
33traceback is printed (unless \exception{SystemExit} is raised) and the
34exception information is saved. After all exit handlers have had a
35chance to run the last exception to be raised is re-raised.
Thomas Wouters89f507f2006-12-13 04:49:30 +000036
37\versionchanged[This function now returns \var{func} which makes it
38 possible to use it as a decorator without binding the
Collin Winter670e6922007-03-21 02:57:17 +000039 original name to \code{None}]{2.6}
40\end{funcdesc}
41
42\begin{funcdesc}{unregister}{func}
43Remove a function \var{func} from the list of functions to be run at
44interpreter-shutdown. After calling \function{unregister()},
45\var{func} is guaranteed not to be called when the interpreter
46shuts down.
47
48\versionadded{3.0}
Fred Drakec19425d2000-06-28 15:07:31 +000049\end{funcdesc}
50
51
Fred Drake1c4efad2000-09-09 03:25:11 +000052\begin{seealso}
53 \seemodule{readline}{Useful example of \module{atexit} to read and
54 write \refmodule{readline} history files.}
55\end{seealso}
56
57
Fred Drakec19425d2000-06-28 15:07:31 +000058\subsection{\module{atexit} Example \label{atexit-example}}
59
60The following simple example demonstrates how a module can initialize
61a counter from a file when it is imported and save the counter's
62updated value automatically when the program terminates without
63relying on the application making an explicit call into this module at
64termination.
65
66\begin{verbatim}
67try:
68 _count = int(open("/tmp/counter").read())
69except IOError:
70 _count = 0
71
72def incrcounter(n):
73 global _count
74 _count = _count + n
75
76def savecounter():
77 open("/tmp/counter", "w").write("%d" % _count)
78
79import atexit
80atexit.register(savecounter)
81\end{verbatim}
Fred Drake2c2068c2003-04-08 17:46:53 +000082
83Positional and keyword arguments may also be passed to
84\function{register()} to be passed along to the registered function
85when it is called:
86
87\begin{verbatim}
88def goodbye(name, adjective):
89 print 'Goodbye, %s, it was %s to meet you.' % (name, adjective)
90
91import atexit
92atexit.register(goodbye, 'Donny', 'nice')
93
94# or:
95atexit.register(goodbye, adjective='nice', name='Donny')
96\end{verbatim}
Thomas Wouters89f507f2006-12-13 04:49:30 +000097
98Usage as a decorator:
99
100\begin{verbatim}
101import atexit
102
103@atexit.register
104def goodbye():
105 print "You are now leaving the Python sector."
106\end{verbatim}
107
108This obviously only works with functions that don't take arguments.