Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1 | |
| 2 | :mod:`atexit` --- Exit handlers |
| 3 | =============================== |
| 4 | |
| 5 | .. module:: atexit |
| 6 | :synopsis: Register and execute cleanup functions. |
Skip Montanaro | 5466246 | 2007-12-08 15:26:16 +0000 | [diff] [blame] | 7 | .. moduleauthor:: Skip Montanaro <skip@pobox.com> |
| 8 | .. sectionauthor:: Skip Montanaro <skip@pobox.com> |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 9 | |
| 10 | |
| 11 | .. versionadded:: 2.0 |
| 12 | |
| 13 | The :mod:`atexit` module defines a single function to register cleanup |
| 14 | functions. Functions thus registered are automatically executed upon normal |
| 15 | interpreter termination. |
| 16 | |
Raymond Hettinger | e0e0822 | 2010-11-06 07:10:31 +0000 | [diff] [blame] | 17 | .. 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 Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 22 | Note: the functions registered via this module are not called when the program |
Georg Brandl | 420cca9 | 2010-11-26 07:21:01 +0000 | [diff] [blame^] | 23 | is killed by a signal not handled by Python, when a Python fatal internal error |
| 24 | is detected, or when :func:`os._exit` is called. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 25 | |
| 26 | .. index:: single: exitfunc (in sys) |
| 27 | |
| 28 | This is an alternate interface to the functionality provided by the |
| 29 | ``sys.exitfunc`` variable. |
| 30 | |
| 31 | Note: This module is unlikely to work correctly when used with other code that |
| 32 | sets ``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 |
| 35 | simplest 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 | |
| 72 | The following simple example demonstrates how a module can initialize a counter |
| 73 | from a file when it is imported and save the counter's updated value |
| 74 | automatically when the program terminates without relying on the application |
| 75 | making 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 | |
| 92 | Positional and keyword arguments may also be passed to :func:`register` to be |
| 93 | passed 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 Brandl | 584265b | 2007-12-02 14:58:50 +0000 | [diff] [blame] | 104 | Usage as a :term:`decorator`:: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 105 | |
| 106 | import atexit |
| 107 | |
| 108 | @atexit.register |
| 109 | def goodbye(): |
| 110 | print "You are now leaving the Python sector." |
| 111 | |
| 112 | This obviously only works with functions that don't take arguments. |
| 113 | |