annotate Lib/atexit.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents ba563ab7a57a
children 4cdf21a0dbbc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13051
f13384105528 Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1 """
f13384105528 Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff changeset
2 atexit.py - allow programmer to define multiple exit functions to be executed
f13384105528 Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff changeset
3 upon normal program termination.
f13384105528 Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff changeset
4
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 13051
diff changeset
5 One public function, register, is defined.
13051
f13384105528 Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff changeset
6 """
f13384105528 Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff changeset
7
16070
b5345e99596b added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents: 15857
diff changeset
8 __all__ = ["register"]
b5345e99596b added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents: 15857
diff changeset
9
34137
f05dd1816c1f SF bug #1083202L UnboundLocalError raised by atexit module
Raymond Hettinger <python@rcn.com>
parents: 33896
diff changeset
10 import sys
f05dd1816c1f SF bug #1083202L UnboundLocalError raised by atexit module
Raymond Hettinger <python@rcn.com>
parents: 33896
diff changeset
11
13051
f13384105528 Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff changeset
12 _exithandlers = []
f13384105528 Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff changeset
13 def _run_exitfuncs():
f13384105528 Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff changeset
14 """run any registered exit functions
f13384105528 Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff changeset
15
f13384105528 Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff changeset
16 _exithandlers is traversed in reverse order so functions are executed
f13384105528 Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff changeset
17 last in, first out.
f13384105528 Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff changeset
18 """
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 13051
diff changeset
19
33896
710b7e8d2ff2 Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents: 31515
diff changeset
20 exc_info = None
13051
f13384105528 Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff changeset
21 while _exithandlers:
16073
713ed6c6bcb5 Bug #128475: mimetools.encode (sometimes) fails when called from a thread.
Tim Peters <tim.peters@gmail.com>
parents: 16070
diff changeset
22 func, targs, kargs = _exithandlers.pop()
33896
710b7e8d2ff2 Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents: 31515
diff changeset
23 try:
710b7e8d2ff2 Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents: 31515
diff changeset
24 func(*targs, **kargs)
710b7e8d2ff2 Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents: 31515
diff changeset
25 except SystemExit:
710b7e8d2ff2 Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents: 31515
diff changeset
26 exc_info = sys.exc_info()
710b7e8d2ff2 Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents: 31515
diff changeset
27 except:
34137
f05dd1816c1f SF bug #1083202L UnboundLocalError raised by atexit module
Raymond Hettinger <python@rcn.com>
parents: 33896
diff changeset
28 import traceback
33896
710b7e8d2ff2 Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents: 31515
diff changeset
29 print >> sys.stderr, "Error in atexit._run_exitfuncs:"
710b7e8d2ff2 Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents: 31515
diff changeset
30 traceback.print_exc()
710b7e8d2ff2 Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents: 31515
diff changeset
31 exc_info = sys.exc_info()
710b7e8d2ff2 Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents: 31515
diff changeset
32
710b7e8d2ff2 Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents: 31515
diff changeset
33 if exc_info is not None:
710b7e8d2ff2 Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents: 31515
diff changeset
34 raise exc_info[0], exc_info[1], exc_info[2]
710b7e8d2ff2 Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents: 31515
diff changeset
35
13051
f13384105528 Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff changeset
36
f13384105528 Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff changeset
37 def register(func, *targs, **kargs):
f13384105528 Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff changeset
38 """register a function to be executed upon normal program termination
f13384105528 Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff changeset
39
f13384105528 Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff changeset
40 func - function to be called at exit
f13384105528 Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff changeset
41 targs - optional arguments to pass to func
f13384105528 Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff changeset
42 kargs - optional keyword arguments to pass to func
40345
ba563ab7a57a Bug #1597824: return the registered function from atexit.register()
Georg Brandl <georg@python.org>
parents: 34137
diff changeset
43
ba563ab7a57a Bug #1597824: return the registered function from atexit.register()
Georg Brandl <georg@python.org>
parents: 34137
diff changeset
44 func is returned to facilitate usage as a decorator.
13051
f13384105528 Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff changeset
45 """
f13384105528 Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff changeset
46 _exithandlers.append((func, targs, kargs))
40345
ba563ab7a57a Bug #1597824: return the registered function from atexit.register()
Georg Brandl <georg@python.org>
parents: 34137
diff changeset
47 return func
13051
f13384105528 Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff changeset
48
24249
4a7b3bac3812 The atexit module effectively turned itself off if sys.exitfunc already
Tim Peters <tim.peters@gmail.com>
parents: 16073
diff changeset
49 if hasattr(sys, "exitfunc"):
4a7b3bac3812 The atexit module effectively turned itself off if sys.exitfunc already
Tim Peters <tim.peters@gmail.com>
parents: 16073
diff changeset
50 # Assume it's another registered exit function - append it to our list
4a7b3bac3812 The atexit module effectively turned itself off if sys.exitfunc already
Tim Peters <tim.peters@gmail.com>
parents: 16073
diff changeset
51 register(sys.exitfunc)
4a7b3bac3812 The atexit module effectively turned itself off if sys.exitfunc already
Tim Peters <tim.peters@gmail.com>
parents: 16073
diff changeset
52 sys.exitfunc = _run_exitfuncs
13051
f13384105528 Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff changeset
53
f13384105528 Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff changeset
54 if __name__ == "__main__":
f13384105528 Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff changeset
55 def x1():
f13384105528 Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff changeset
56 print "running x1"
f13384105528 Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff changeset
57 def x2(n):
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 28089
diff changeset
58 print "running x2(%r)" % (n,)
13051
f13384105528 Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff changeset
59 def x3(n, kwd=None):
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 28089
diff changeset
60 print "running x3(%r, kwd=%r)" % (n, kwd)
13051
f13384105528 Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff changeset
61
f13384105528 Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff changeset
62 register(x1)
f13384105528 Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff changeset
63 register(x2, 12)
f13384105528 Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff changeset
64 register(x3, 5, "bar")
f13384105528 Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff changeset
65 register(x3, "no kwd args")