blob: 9798b5750744b1e80abd87b13b899b9f6d82b6ab [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
4\declaremodule{standard}{atexit}
5\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 +000011The \module{atexit} module defines a single function to register
12cleanup functions. Functions thus registered are automatically
13executed upon normal interpreter termination.
14
15Note: the functions registered via this module are not called when the program is killed by a
16signal, when a Python fatal internal error is detected, or when
Fred Drake3c62d9e2000-07-06 04:51:04 +000017\function{os._exit()} is called.
Fred Drakec19425d2000-06-28 15:07:31 +000018
19This is an alternate interface to the functionality provided by the
20\code{sys.exitfunc} variable.
21\withsubitem{(in sys)}{\ttindex{exitfunc}}
22
Skip Montanaro09151652000-07-05 23:11:26 +000023Note: This module is unlikely to work correctly when used with other code
24that sets \code{sys.exitfunc}. In particular, other core Python modules are
25free to use \module{atexit} without the programmer's knowledge. Authors who
26use \code{sys.exitfunc} should convert their code to use
27\module{atexit} instead. The simplest way to convert code that sets
28\code{sys.exitfunc} is to import \module{atexit} and register the function
29that had been bound to \code{sys.exitfunc}.
30
Fred Drakec19425d2000-06-28 15:07:31 +000031\begin{funcdesc}{register}{func\optional{, *args\optional{, **kargs}}}
32Register \var{func} as a function to be executed at termination. Any
33optional arguments that are to be passed to \var{func} must be passed
34as arguments to \function{register()}.
35
36At normal program termination (for instance, if
37\function{sys.exit()} is called or the main module's execution
38completes), all functions registered are called in last in, first out
39order. The assumption is that lower level modules will normally be
40imported before higher level modules and thus must be cleaned up
41later.
Skip Montanaro599bd5e2004-11-04 04:31:30 +000042
Fred Drake99e5ce52004-11-04 05:45:44 +000043If an exception is raised during execution of the exit handlers, a
44traceback is printed (unless \exception{SystemExit} is raised) and the
45exception information is saved. After all exit handlers have had a
46chance to run the last exception to be raised is re-raised.
Georg Brandl54082112006-11-16 16:50:59 +000047
48\versionchanged[This function now returns \var{func} which makes it
49 possible to use it as a decorator without binding the
50 original name to \code{None}]{2.6}
Fred Drakec19425d2000-06-28 15:07:31 +000051\end{funcdesc}
52
53
Fred Drake1c4efad2000-09-09 03:25:11 +000054\begin{seealso}
55 \seemodule{readline}{Useful example of \module{atexit} to read and
56 write \refmodule{readline} history files.}
57\end{seealso}
58
59
Fred Drakec19425d2000-06-28 15:07:31 +000060\subsection{\module{atexit} Example \label{atexit-example}}
61
62The following simple example demonstrates how a module can initialize
63a counter from a file when it is imported and save the counter's
64updated value automatically when the program terminates without
65relying on the application making an explicit call into this module at
66termination.
67
68\begin{verbatim}
69try:
70 _count = int(open("/tmp/counter").read())
71except IOError:
72 _count = 0
73
74def incrcounter(n):
75 global _count
76 _count = _count + n
77
78def savecounter():
79 open("/tmp/counter", "w").write("%d" % _count)
80
81import atexit
82atexit.register(savecounter)
83\end{verbatim}
Fred Drake2c2068c2003-04-08 17:46:53 +000084
85Positional and keyword arguments may also be passed to
86\function{register()} to be passed along to the registered function
87when it is called:
88
89\begin{verbatim}
90def goodbye(name, adjective):
91 print 'Goodbye, %s, it was %s to meet you.' % (name, adjective)
92
93import atexit
94atexit.register(goodbye, 'Donny', 'nice')
95
96# or:
97atexit.register(goodbye, adjective='nice', name='Donny')
98\end{verbatim}
Georg Brandl54082112006-11-16 16:50:59 +000099
100Usage as a decorator:
101
102\begin{verbatim}
103import atexit
104
105@atexit.register
106def goodbye():
107 print "You are now leaving the Python sector."
108\end{verbatim}
109
110This obviously only works with functions that don't take arguments.