2002-07-17 03:30:59 +08:00
|
|
|
# Test the atexit module.
|
2002-11-06 19:37:57 +08:00
|
|
|
from test.test_support import TESTFN, vereq, is_jython
|
2002-07-17 03:30:59 +08:00
|
|
|
import atexit
|
2002-07-26 19:33:49 +08:00
|
|
|
from os import popen, unlink
|
2002-07-17 08:34:26 +08:00
|
|
|
import sys
|
2002-07-17 03:30:59 +08:00
|
|
|
|
2002-11-06 19:37:57 +08:00
|
|
|
executable = sys.executable
|
|
|
|
if is_jython:
|
|
|
|
executable = "jython"
|
|
|
|
|
2002-07-17 03:30:59 +08:00
|
|
|
input = """\
|
2000-06-28 23:07:31 +08:00
|
|
|
import atexit
|
|
|
|
|
|
|
|
def handler1():
|
|
|
|
print "handler1"
|
|
|
|
|
|
|
|
def handler2(*args, **kargs):
|
|
|
|
print "handler2", args, kargs
|
|
|
|
|
|
|
|
atexit.register(handler1)
|
|
|
|
atexit.register(handler2)
|
|
|
|
atexit.register(handler2, 7, kw="abc")
|
2002-07-17 03:30:59 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
fname = TESTFN + ".py"
|
|
|
|
f = file(fname, "w")
|
|
|
|
f.write(input)
|
|
|
|
f.close()
|
|
|
|
|
2002-11-06 19:37:57 +08:00
|
|
|
p = popen("%s %s" % (executable, fname))
|
2002-07-17 03:30:59 +08:00
|
|
|
output = p.read()
|
|
|
|
p.close()
|
|
|
|
vereq(output, """\
|
|
|
|
handler2 (7,) {'kw': 'abc'}
|
|
|
|
handler2 () {}
|
|
|
|
handler1
|
|
|
|
""")
|
|
|
|
|
|
|
|
input = """\
|
|
|
|
def direct():
|
|
|
|
print "direct exit"
|
|
|
|
|
|
|
|
import sys
|
|
|
|
sys.exitfunc = direct
|
|
|
|
|
2002-07-17 05:35:23 +08:00
|
|
|
# Make sure atexit doesn't drop
|
2002-07-17 03:30:59 +08:00
|
|
|
def indirect():
|
|
|
|
print "indirect exit"
|
|
|
|
|
|
|
|
import atexit
|
|
|
|
atexit.register(indirect)
|
|
|
|
"""
|
|
|
|
|
|
|
|
f = file(fname, "w")
|
|
|
|
f.write(input)
|
|
|
|
f.close()
|
2000-06-28 23:07:31 +08:00
|
|
|
|
2002-11-06 19:37:57 +08:00
|
|
|
p = popen("%s %s" % (executable, fname))
|
2002-07-17 03:30:59 +08:00
|
|
|
output = p.read()
|
|
|
|
p.close()
|
|
|
|
vereq(output, """\
|
|
|
|
indirect exit
|
|
|
|
direct exit
|
|
|
|
""")
|
2000-06-28 23:07:31 +08:00
|
|
|
|
2002-07-26 19:33:49 +08:00
|
|
|
unlink(fname)
|