Skip to content

Commit

Permalink
The atexit module effectively turned itself off if sys.exitfunc already
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tim-one committed Jul 16, 2002
1 parent 32a0396 commit 012b69c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 24 deletions.
14 changes: 5 additions & 9 deletions Lib/atexit.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,11 @@ def register(func, *targs, **kargs):
_exithandlers.append((func, targs, kargs))

import sys
try:
x = sys.exitfunc
except AttributeError:
sys.exitfunc = _run_exitfuncs
else:
# if x isn't our own exit func executive, assume it's another
# registered exit function - append it to our list...
if x != _run_exitfuncs:
register(x)
if hasattr(sys, "exitfunc"):
# Assume it's another registered exit function - append it to our list
register(sys.exitfunc)
sys.exitfunc = _run_exitfuncs

del sys

if __name__ == "__main__":
Expand Down
4 changes: 0 additions & 4 deletions Lib/test/output/test_atexit

This file was deleted.

59 changes: 48 additions & 11 deletions Lib/test/test_atexit.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Test the exit module
from test_support import verbose
# Test the atexit module.
from test_support import TESTFN, vereq
import atexit
import os

input = """\
import atexit
def handler1():
Expand All @@ -8,17 +12,50 @@ def handler1():
def handler2(*args, **kargs):
print "handler2", args, kargs
# save any exit functions that may have been registered as part of the
# test framework
_exithandlers = atexit._exithandlers
atexit._exithandlers = []

atexit.register(handler1)
atexit.register(handler2)
atexit.register(handler2, 7, kw="abc")
"""

fname = TESTFN + ".py"
f = file(fname, "w")
f.write(input)
f.close()

p = os.popen("python " + fname)
output = p.read()
p.close()
vereq(output, """\
handler2 (7,) {'kw': 'abc'}
handler2 () {}
handler1
""")

input = """\
def direct():
print "direct exit"
import sys
sys.exitfunc = direct
# Make sure atexit doesn't drop
def indirect():
print "indirect exit"
import atexit
atexit.register(indirect)
"""

f = file(fname, "w")
f.write(input)
f.close()

# simulate exit behavior by calling atexit._run_exitfuncs directly...
atexit._run_exitfuncs()
p = os.popen("python " + fname)
output = p.read()
p.close()
vereq(output, """\
indirect exit
direct exit
""")

# restore exit handlers
atexit._exithandlers = _exithandlers
os.unlink(fname)

0 comments on commit 012b69c

Please sign in to comment.