Fred Drake | c19425d | 2000-06-28 15:07:31 +0000 | [diff] [blame] | 1 | \section{\module{atexit} --- |
Fred Drake | 3c62d9e | 2000-07-06 04:51:04 +0000 | [diff] [blame] | 2 | Exit handlers} |
Fred Drake | c19425d | 2000-06-28 15:07:31 +0000 | [diff] [blame] | 3 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 4 | \declaremodule{builtin}{atexit} |
Fred Drake | c19425d | 2000-06-28 15:07:31 +0000 | [diff] [blame] | 5 | \moduleauthor{Skip Montanaro}{skip@mojam.com} |
| 6 | \sectionauthor{Skip Montanaro}{skip@mojam.com} |
| 7 | \modulesynopsis{Register and execute cleanup functions.} |
| 8 | |
Fred Drake | 30f76ff | 2000-06-30 16:06:19 +0000 | [diff] [blame] | 9 | \versionadded{2.0} |
Fred Drake | be93a83 | 2000-06-28 22:07:55 +0000 | [diff] [blame] | 10 | |
Fred Drake | c19425d | 2000-06-28 15:07:31 +0000 | [diff] [blame] | 11 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 12 | The \module{atexit} module defines functions to register and |
| 13 | unregister cleanup functions. Functions thus registered are |
| 14 | automatically executed upon normal interpreter termination. |
Fred Drake | c19425d | 2000-06-28 15:07:31 +0000 | [diff] [blame] | 15 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 16 | Note: the functions registered via this module are not called when |
| 17 | the program is killed by a signal, when a Python fatal internal |
| 18 | error is detected, or when \function{os._exit()} is called. |
Skip Montanaro | 0915165 | 2000-07-05 23:11:26 +0000 | [diff] [blame] | 19 | |
Fred Drake | c19425d | 2000-06-28 15:07:31 +0000 | [diff] [blame] | 20 | \begin{funcdesc}{register}{func\optional{, *args\optional{, **kargs}}} |
| 21 | Register \var{func} as a function to be executed at termination. Any |
| 22 | optional arguments that are to be passed to \var{func} must be passed |
| 23 | as arguments to \function{register()}. |
| 24 | |
| 25 | At normal program termination (for instance, if |
| 26 | \function{sys.exit()} is called or the main module's execution |
| 27 | completes), all functions registered are called in last in, first out |
| 28 | order. The assumption is that lower level modules will normally be |
| 29 | imported before higher level modules and thus must be cleaned up |
| 30 | later. |
Skip Montanaro | 599bd5e | 2004-11-04 04:31:30 +0000 | [diff] [blame] | 31 | |
Fred Drake | 99e5ce5 | 2004-11-04 05:45:44 +0000 | [diff] [blame] | 32 | If an exception is raised during execution of the exit handlers, a |
| 33 | traceback is printed (unless \exception{SystemExit} is raised) and the |
| 34 | exception information is saved. After all exit handlers have had a |
| 35 | chance to run the last exception to be raised is re-raised. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 36 | |
| 37 | \versionchanged[This function now returns \var{func} which makes it |
| 38 | possible to use it as a decorator without binding the |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 39 | original name to \code{None}]{2.6} |
| 40 | \end{funcdesc} |
| 41 | |
| 42 | \begin{funcdesc}{unregister}{func} |
| 43 | Remove a function \var{func} from the list of functions to be run at |
| 44 | interpreter-shutdown. After calling \function{unregister()}, |
| 45 | \var{func} is guaranteed not to be called when the interpreter |
| 46 | shuts down. |
| 47 | |
| 48 | \versionadded{3.0} |
Fred Drake | c19425d | 2000-06-28 15:07:31 +0000 | [diff] [blame] | 49 | \end{funcdesc} |
| 50 | |
| 51 | |
Fred Drake | 1c4efad | 2000-09-09 03:25:11 +0000 | [diff] [blame] | 52 | \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 Drake | c19425d | 2000-06-28 15:07:31 +0000 | [diff] [blame] | 58 | \subsection{\module{atexit} Example \label{atexit-example}} |
| 59 | |
| 60 | The following simple example demonstrates how a module can initialize |
| 61 | a counter from a file when it is imported and save the counter's |
| 62 | updated value automatically when the program terminates without |
| 63 | relying on the application making an explicit call into this module at |
| 64 | termination. |
| 65 | |
| 66 | \begin{verbatim} |
| 67 | try: |
| 68 | _count = int(open("/tmp/counter").read()) |
| 69 | except IOError: |
| 70 | _count = 0 |
| 71 | |
| 72 | def incrcounter(n): |
| 73 | global _count |
| 74 | _count = _count + n |
| 75 | |
| 76 | def savecounter(): |
| 77 | open("/tmp/counter", "w").write("%d" % _count) |
| 78 | |
| 79 | import atexit |
| 80 | atexit.register(savecounter) |
| 81 | \end{verbatim} |
Fred Drake | 2c2068c | 2003-04-08 17:46:53 +0000 | [diff] [blame] | 82 | |
| 83 | Positional and keyword arguments may also be passed to |
| 84 | \function{register()} to be passed along to the registered function |
| 85 | when it is called: |
| 86 | |
| 87 | \begin{verbatim} |
| 88 | def goodbye(name, adjective): |
| 89 | print 'Goodbye, %s, it was %s to meet you.' % (name, adjective) |
| 90 | |
| 91 | import atexit |
| 92 | atexit.register(goodbye, 'Donny', 'nice') |
| 93 | |
| 94 | # or: |
| 95 | atexit.register(goodbye, adjective='nice', name='Donny') |
| 96 | \end{verbatim} |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 97 | |
| 98 | Usage as a decorator: |
| 99 | |
| 100 | \begin{verbatim} |
| 101 | import atexit |
| 102 | |
| 103 | @atexit.register |
| 104 | def goodbye(): |
| 105 | print "You are now leaving the Python sector." |
| 106 | \end{verbatim} |
| 107 | |
| 108 | This obviously only works with functions that don't take arguments. |