/Doc/library/atexit.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 108 lines · 73 code · 35 blank · 0 comment · 0 complexity · d7da255e226a6bb3ac94802858c267e4 MD5 · raw file

  1. :mod:`atexit` --- Exit handlers
  2. ===============================
  3. .. module:: atexit
  4. :synopsis: Register and execute cleanup functions.
  5. .. moduleauthor:: Skip Montanaro <skip@pobox.com>
  6. .. sectionauthor:: Skip Montanaro <skip@pobox.com>
  7. .. versionadded:: 2.0
  8. The :mod:`atexit` module defines a single function to register cleanup
  9. functions. Functions thus registered are automatically executed upon normal
  10. interpreter termination.
  11. Note: the functions registered via this module are not called when the program
  12. is killed by a signal, when a Python fatal internal error is detected, or when
  13. :func:`os._exit` is called.
  14. .. index:: single: exitfunc (in sys)
  15. This is an alternate interface to the functionality provided by the
  16. ``sys.exitfunc`` variable.
  17. Note: This module is unlikely to work correctly when used with other code that
  18. sets ``sys.exitfunc``. In particular, other core Python modules are free to use
  19. :mod:`atexit` without the programmer's knowledge. Authors who use
  20. ``sys.exitfunc`` should convert their code to use :mod:`atexit` instead. The
  21. simplest way to convert code that sets ``sys.exitfunc`` is to import
  22. :mod:`atexit` and register the function that had been bound to ``sys.exitfunc``.
  23. .. function:: register(func[, *args[, **kargs]])
  24. Register *func* as a function to be executed at termination. Any optional
  25. arguments that are to be passed to *func* must be passed as arguments to
  26. :func:`register`.
  27. At normal program termination (for instance, if :func:`sys.exit` is called or
  28. the main module's execution completes), all functions registered are called in
  29. last in, first out order. The assumption is that lower level modules will
  30. normally be imported before higher level modules and thus must be cleaned up
  31. later.
  32. If an exception is raised during execution of the exit handlers, a traceback is
  33. printed (unless :exc:`SystemExit` is raised) and the exception information is
  34. saved. After all exit handlers have had a chance to run the last exception to
  35. be raised is re-raised.
  36. .. versionchanged:: 2.6
  37. This function now returns *func* which makes it possible to use it as a
  38. decorator without binding the original name to ``None``.
  39. .. seealso::
  40. Module :mod:`readline`
  41. Useful example of :mod:`atexit` to read and write :mod:`readline` history files.
  42. .. _atexit-example:
  43. :mod:`atexit` Example
  44. ---------------------
  45. The following simple example demonstrates how a module can initialize a counter
  46. from a file when it is imported and save the counter's updated value
  47. automatically when the program terminates without relying on the application
  48. making an explicit call into this module at termination. ::
  49. try:
  50. _count = int(open("/tmp/counter").read())
  51. except IOError:
  52. _count = 0
  53. def incrcounter(n):
  54. global _count
  55. _count = _count + n
  56. def savecounter():
  57. open("/tmp/counter", "w").write("%d" % _count)
  58. import atexit
  59. atexit.register(savecounter)
  60. Positional and keyword arguments may also be passed to :func:`register` to be
  61. passed along to the registered function when it is called::
  62. def goodbye(name, adjective):
  63. print 'Goodbye, %s, it was %s to meet you.' % (name, adjective)
  64. import atexit
  65. atexit.register(goodbye, 'Donny', 'nice')
  66. # or:
  67. atexit.register(goodbye, adjective='nice', name='Donny')
  68. Usage as a :term:`decorator`::
  69. import atexit
  70. @atexit.register
  71. def goodbye():
  72. print "You are now leaving the Python sector."
  73. This obviously only works with functions that don't take arguments.