"atexit" — 終了ハンドラ
***********************

バージョン 2.0 で追加.

**Source code:** Lib/atexit.py

======================================================================

The "atexit" module defines a single function to register cleanup
functions.  Functions thus registered are automatically executed upon
normal interpreter termination.  "atexit" runs these functions in the
*reverse* order in which they were registered; if you register "A",
"B", and "C", at interpreter termination time they will be run in the
order "C", "B", "A".

**注意:** このモジュールを使用して登録された関数は、プログラムが
Python が扱わないシグナルによって kill された場合、Python 内部で致命的
なエラーが検出された場合、あるいは "os._exit()" が呼び出された場合は実
行されません。

This is an alternate interface to the functionality provided by the
"sys.exitfunc()" variable.

Note: This module is unlikely to work correctly when used with other
code that sets "sys.exitfunc".  In particular, other core Python
modules are free to use "atexit" without the programmer’s knowledge.
Authors who use "sys.exitfunc" should convert their code to use
"atexit" instead.  The simplest way to convert code that sets
"sys.exitfunc" is to import "atexit" and register the function that
had been bound to "sys.exitfunc".

atexit.register(func[, *args[, **kwargs]])

   *func* を終了時に実行する関数として登録します。*func* に渡す引数は
   "register()" の引数として指定しなければなりません。同じ関数を同じ引
   数で複数回登録できます。

   通常のプログラムの終了時、例えば "sys.exit()" が呼び出されるとき、
   あるいは、メインモジュールの実行が完了したときに、登録された全ての
   関数を、最後に登録されたものから順に呼び出します。通常、より低レベ
   ルのモジュールはより高レベルのモジュールより前に import されるので
   、後で後始末が行われるという仮定に基づいています。

   終了ハンドラの実行中に例外が発生すると、("SystemExit" 以外の場合は)
   トレースバックを表示して、例外の情報を保存します。全ての終了ハンド
   ラに動作するチャンスを与えた後に、最後に送出された例外を再送出しま
   す。

   バージョン 2.6 で変更: This function now returns *func*, which
   makes it possible to use it as a decorator.

参考:

  "readline" モジュール
     "readline" ヒストリファイルを読み書きするための "atexit" の有用な
     例です。


"atexit" の例
=============

次の簡単な例では、あるモジュールを import した時にカウンタを初期化して
おき、プログラムが終了するときにアプリケーションがこのモジュールを明示
的に呼び出さなくてもカウンタが更新されるようにする方法を示しています。

   try:
       _count = int(open("counter").read())
   except IOError:
       _count = 0

   def incrcounter(n):
       global _count
       _count = _count + n

   def savecounter():
       open("counter", "w").write("%d" % _count)

   import atexit
   atexit.register(savecounter)

"register()" に指定した位置引数とキーワード引数は登録した関数を呼び出
す際に渡されます:

   def goodbye(name, adjective):
       print 'Goodbye, %s, it was %s to meet you.' % (name, adjective)

   import atexit
   atexit.register(goodbye, 'Donny', 'nice')

   # or:
   atexit.register(goodbye, adjective='nice', name='Donny')

*デコレータ* として利用する例:

   import atexit

   @atexit.register
   def goodbye():
       print "You are now leaving the Python sector."

デコレータとして利用できるのは、その関数が引数なしで呼び出された場合に
限られます。
