Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 012b69c

Browse files
committed
The atexit module effectively turned itself off if sys.exitfunc already
existed at the time atexit first got imported. That's a bug, and this fixes it. Also reworked test_atexit.py to test for this too, and to stop using an "expected output" file, and to test what actually happens at exit instead of just simulating what it thinks atexit will do at exit. Bugfix candidate, but it's messy so I'll backport to 2.2 myself.
1 parent 32a0396 commit 012b69c

3 files changed

Lines changed: 53 additions & 24 deletions

File tree

Lib/atexit.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,11 @@ def register(func, *targs, **kargs):
2929
_exithandlers.append((func, targs, kargs))
3030

3131
import sys
32-
try:
33-
x = sys.exitfunc
34-
except AttributeError:
35-
sys.exitfunc = _run_exitfuncs
36-
else:
37-
# if x isn't our own exit func executive, assume it's another
38-
# registered exit function - append it to our list...
39-
if x != _run_exitfuncs:
40-
register(x)
32+
if hasattr(sys, "exitfunc"):
33+
# Assume it's another registered exit function - append it to our list
34+
register(sys.exitfunc)
35+
sys.exitfunc = _run_exitfuncs
36+
4137
del sys
4238

4339
if __name__ == "__main__":

Lib/test/output/test_atexit

Lines changed: 0 additions & 4 deletions
This file was deleted.

Lib/test/test_atexit.py

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
# Test the exit module
2-
from test_support import verbose
1+
# Test the atexit module.
2+
from test_support import TESTFN, vereq
3+
import atexit
4+
import os
5+
6+
input = """\
37
import atexit
48
59
def handler1():
@@ -8,17 +12,50 @@ def handler1():
812
def handler2(*args, **kargs):
913
print "handler2", args, kargs
1014
11-
# save any exit functions that may have been registered as part of the
12-
# test framework
13-
_exithandlers = atexit._exithandlers
14-
atexit._exithandlers = []
15-
1615
atexit.register(handler1)
1716
atexit.register(handler2)
1817
atexit.register(handler2, 7, kw="abc")
18+
"""
19+
20+
fname = TESTFN + ".py"
21+
f = file(fname, "w")
22+
f.write(input)
23+
f.close()
24+
25+
p = os.popen("python " + fname)
26+
output = p.read()
27+
p.close()
28+
vereq(output, """\
29+
handler2 (7,) {'kw': 'abc'}
30+
handler2 () {}
31+
handler1
32+
""")
33+
34+
input = """\
35+
def direct():
36+
print "direct exit"
37+
38+
import sys
39+
sys.exitfunc = direct
40+
41+
# Make sure atexit doesn't drop
42+
def indirect():
43+
print "indirect exit"
44+
45+
import atexit
46+
atexit.register(indirect)
47+
"""
48+
49+
f = file(fname, "w")
50+
f.write(input)
51+
f.close()
1952

20-
# simulate exit behavior by calling atexit._run_exitfuncs directly...
21-
atexit._run_exitfuncs()
53+
p = os.popen("python " + fname)
54+
output = p.read()
55+
p.close()
56+
vereq(output, """\
57+
indirect exit
58+
direct exit
59+
""")
2260

23-
# restore exit handlers
24-
atexit._exithandlers = _exithandlers
61+
os.unlink(fname)

0 commit comments

Comments
 (0)