New in version 2.0.
Theatexit module defines a single function to registercleanup functions. Functions thus registered are automaticallyexecuted upon normal interpreter termination.
Note: the functions registered via this module are not called when the program is killed by asignal, when a Python fatal internal error is detected, or whenos._exit() is called.
This is an alternate interface to the functionality provided by thesys.exitfunc variable.
Note: This module is unlikely to work correctly when used with other codethat setssys.exitfunc. In particular, other core Python modules arefree to useatexit without the programmer's knowledge. Authors whousesys.exitfunc should convert their code to useatexit instead. The simplest way to convert code that setssys.exitfunc is to importatexit and register the functionthat had been bound tosys.exitfunc.
| func[, *args[, **kargs]]) |
At normal program termination (for instance, ifsys.exit() is called or the main module's executioncompletes), all functions registered are called in last in, first outorder. The assumption is that lower level modules will normally beimported before higher level modules and thus must be cleaned uplater.
If an exception is raised during execution of the exit handlers, atraceback is printed (unlessSystemExit is raised) and theexception information is saved. After all exit handlers have had achance to run the last exception to be raised is re-raised.