blob: eab8cd9cca34acf376c0b15a9c2f15c1ef845661 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001
2:mod:`atexit` --- Exit handlers
3===============================
4
5.. module:: atexit
6 :synopsis: Register and execute cleanup functions.
Skip Montanaro54662462007-12-08 15:26:16 +00007.. moduleauthor:: Skip Montanaro <skip@pobox.com>
8.. sectionauthor:: Skip Montanaro <skip@pobox.com>
Georg Brandl8ec7f652007-08-15 14:28:01 +00009
10
11.. versionadded:: 2.0
12
13The :mod:`atexit` module defines a single function to register cleanup
14functions. Functions thus registered are automatically executed upon normal
15interpreter termination.
16
Raymond Hettingere0e08222010-11-06 07:10:31 +000017.. seealso::
18
19 Latest version of the `atexit Python source code
20 <http://svn.python.org/view/python/branches/release27-maint/Lib/atexit.py?view=markup>`_
21
Georg Brandl8ec7f652007-08-15 14:28:01 +000022Note: the functions registered via this module are not called when the program
Georg Brandl420cca92010-11-26 07:21:01 +000023is killed by a signal not handled by Python, when a Python fatal internal error
24is detected, or when :func:`os._exit` is called.
Georg Brandl8ec7f652007-08-15 14:28:01 +000025
26.. index:: single: exitfunc (in sys)
27
28This is an alternate interface to the functionality provided by the
29``sys.exitfunc`` variable.
30
31Note: This module is unlikely to work correctly when used with other code that
32sets ``sys.exitfunc``. In particular, other core Python modules are free to use
33:mod:`atexit` without the programmer's knowledge. Authors who use
34``sys.exitfunc`` should convert their code to use :mod:`atexit` instead. The
35simplest way to convert code that sets ``sys.exitfunc`` is to import
36:mod:`atexit` and register the function that had been bound to ``sys.exitfunc``.
37
38
39.. function:: register(func[, *args[, **kargs]])
40
41 Register *func* as a function to be executed at termination. Any optional
42 arguments that are to be passed to *func* must be passed as arguments to
43 :func:`register`.
44
45 At normal program termination (for instance, if :func:`sys.exit` is called or
46 the main module's execution completes), all functions registered are called in
47 last in, first out order. The assumption is that lower level modules will
48 normally be imported before higher level modules and thus must be cleaned up
49 later.
50
51 If an exception is raised during execution of the exit handlers, a traceback is
52 printed (unless :exc:`SystemExit` is raised) and the exception information is
53 saved. After all exit handlers have had a chance to run the last exception to
54 be raised is re-raised.
55
56 .. versionchanged:: 2.6
57 This function now returns *func* which makes it possible to use it as a
58 decorator without binding the original name to ``None``.
59
60
61.. seealso::
62
63 Module :mod:`readline`
64 Useful example of :mod:`atexit` to read and write :mod:`readline` history files.
65
66
67.. _atexit-example:
68
69:mod:`atexit` Example
70---------------------
71
72The following simple example demonstrates how a module can initialize a counter
73from a file when it is imported and save the counter's updated value
74automatically when the program terminates without relying on the application
75making an explicit call into this module at termination. ::
76
77 try:
78 _count = int(open("/tmp/counter").read())
79 except IOError:
80 _count = 0
81
82 def incrcounter(n):
83 global _count
84 _count = _count + n
85
86 def savecounter():
87 open("/tmp/counter", "w").write("%d" % _count)
88
89 import atexit
90 atexit.register(savecounter)
91
92Positional and keyword arguments may also be passed to :func:`register` to be
93passed along to the registered function when it is called::
94
95 def goodbye(name, adjective):
96 print 'Goodbye, %s, it was %s to meet you.' % (name, adjective)
97
98 import atexit
99 atexit.register(goodbye, 'Donny', 'nice')
100
101 # or:
102 atexit.register(goodbye, adjective='nice', name='Donny')
103
Georg Brandl584265b2007-12-02 14:58:50 +0000104Usage as a :term:`decorator`::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000105
106 import atexit
107
108 @atexit.register
109 def goodbye():
110 print "You are now leaving the Python sector."
111
112This obviously only works with functions that don't take arguments.
113