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 | |
| 17 | Note: the functions registered via this module are not called when the program |
| 18 | is killed by a signal, when a Python fatal internal error is detected, or when |
| 19 | :func:`os._exit` is called. |
| 20 | |
| 21 | .. index:: single: exitfunc (in sys) |
| 22 | |
| 23 | This is an alternate interface to the functionality provided by the |
| 24 | ``sys.exitfunc`` variable. |
| 25 | |
| 26 | Note: This module is unlikely to work correctly when used with other code that |
| 27 | sets ``sys.exitfunc``. In particular, other core Python modules are free to use |
| 28 | :mod:`atexit` without the programmer's knowledge. Authors who use |
| 29 | ``sys.exitfunc`` should convert their code to use :mod:`atexit` instead. The |
| 30 | simplest way to convert code that sets ``sys.exitfunc`` is to import |
| 31 | :mod:`atexit` and register the function that had been bound to ``sys.exitfunc``. |
| 32 | |
| 33 | |
| 34 | .. function:: register(func[, *args[, **kargs]]) |
| 35 | |
| 36 | Register *func* as a function to be executed at termination. Any optional |
| 37 | arguments that are to be passed to *func* must be passed as arguments to |
| 38 | :func:`register`. |
| 39 | |
| 40 | At normal program termination (for instance, if :func:`sys.exit` is called or |
| 41 | the main module's execution completes), all functions registered are called in |
| 42 | last in, first out order. The assumption is that lower level modules will |
| 43 | normally be imported before higher level modules and thus must be cleaned up |
| 44 | later. |
| 45 | |
| 46 | If an exception is raised during execution of the exit handlers, a traceback is |
| 47 | printed (unless :exc:`SystemExit` is raised) and the exception information is |
| 48 | saved. After all exit handlers have had a chance to run the last exception to |
| 49 | be raised is re-raised. |
| 50 | |
| 51 | .. versionchanged:: 2.6 |
| 52 | This function now returns *func* which makes it possible to use it as a |
| 53 | decorator without binding the original name to ``None``. |
| 54 | |
| 55 | |
| 56 | .. seealso:: |
| 57 | |
| 58 | Module :mod:`readline` |
| 59 | Useful example of :mod:`atexit` to read and write :mod:`readline` history files. |
| 60 | |
| 61 | |
| 62 | .. _atexit-example: |
| 63 | |
| 64 | :mod:`atexit` Example |
| 65 | --------------------- |
| 66 | |
| 67 | The following simple example demonstrates how a module can initialize a counter |
| 68 | from a file when it is imported and save the counter's updated value |
| 69 | automatically when the program terminates without relying on the application |
| 70 | making an explicit call into this module at termination. :: |
| 71 | |
| 72 | try: |
| 73 | _count = int(open("/tmp/counter").read()) |
| 74 | except IOError: |
| 75 | _count = 0 |
| 76 | |
| 77 | def incrcounter(n): |
| 78 | global _count |
| 79 | _count = _count + n |
| 80 | |
| 81 | def savecounter(): |
| 82 | open("/tmp/counter", "w").write("%d" % _count) |
| 83 | |
| 84 | import atexit |
| 85 | atexit.register(savecounter) |
| 86 | |
| 87 | Positional and keyword arguments may also be passed to :func:`register` to be |
| 88 | passed along to the registered function when it is called:: |
| 89 | |
| 90 | def goodbye(name, adjective): |
| 91 | print 'Goodbye, %s, it was %s to meet you.' % (name, adjective) |
| 92 | |
| 93 | import atexit |
| 94 | atexit.register(goodbye, 'Donny', 'nice') |
| 95 | |
| 96 | # or: |
| 97 | atexit.register(goodbye, adjective='nice', name='Donny') |
| 98 | |
Georg Brandl | 584265b | 2007-12-02 14:58:50 +0000 | [diff] [blame] | 99 | Usage as a :term:`decorator`:: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 100 | |
| 101 | import atexit |
| 102 | |
| 103 | @atexit.register |
| 104 | def goodbye(): |
| 105 | print "You are now leaving the Python sector." |
| 106 | |
| 107 | This obviously only works with functions that don't take arguments. |
| 108 | |