2002-07-16 16:30:59 -03:00
|
|
|
# Test the atexit module.
|
2002-11-06 07:37:57 -04:00
|
|
|
from test.test_support import TESTFN, vereq, is_jython
|
2002-07-16 16:30:59 -03:00
|
|
|
import atexit
|
2002-07-26 08:33:49 -03:00
|
|
|
from os import popen, unlink
|
2002-07-16 21:34:26 -03:00
|
|
|
import sys
|
2002-07-16 16:30:59 -03:00
|
|
|
|
2002-11-06 07:37:57 -04:00
|
|
|
executable = sys.executable
|
|
|
|
if is_jython:
|
|
|
|
executable = "jython"
|
|
|
|
|
2002-07-16 16:30:59 -03:00
|
|
|
input = """\
|
2000-06-28 12:07:31 -03: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-16 16:30:59 -03:00
|
|
|
"""
|
|
|
|
|
|
|
|
fname = TESTFN + ".py"
|
|
|
|
f = file(fname, "w")
|
|
|
|
f.write(input)
|
|
|
|
f.close()
|
|
|
|
|
2002-11-06 07:37:57 -04:00
|
|
|
p = popen("%s %s" % (executable, fname))
|
2002-07-16 16:30:59 -03: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-16 18:35:23 -03:00
|
|
|
# Make sure atexit doesn't drop
|
2002-07-16 16:30:59 -03:00
|
|
|
def indirect():
|
|
|
|
print "indirect exit"
|
|
|
|
|
|
|
|
import atexit
|
|
|
|
atexit.register(indirect)
|
|
|
|
"""
|
|
|
|
|
|
|
|
f = file(fname, "w")
|
|
|
|
f.write(input)
|
|
|
|
f.close()
|
2000-06-28 12:07:31 -03:00
|
|
|
|
2002-11-06 07:37:57 -04:00
|
|
|
p = popen("%s %s" % (executable, fname))
|
2002-07-16 16:30:59 -03:00
|
|
|
output = p.read()
|
|
|
|
p.close()
|
|
|
|
vereq(output, """\
|
|
|
|
indirect exit
|
|
|
|
direct exit
|
|
|
|
""")
|
2000-06-28 12:07:31 -03:00
|
|
|
|
2002-07-26 08:33:49 -03:00
|
|
|
unlink(fname)
|