2002-07-16 21:34:26 -03:00
|
|
|
import sys
|
2004-11-04 00:31:30 -04:00
|
|
|
import unittest
|
|
|
|
import StringIO
|
2000-06-28 12:07:31 -03:00
|
|
|
import atexit
|
2004-11-04 00:31:30 -04:00
|
|
|
from test import test_support
|
2000-06-28 12:07:31 -03:00
|
|
|
|
2004-11-04 00:31:30 -04:00
|
|
|
class TestCase(unittest.TestCase):
|
|
|
|
def test_args(self):
|
|
|
|
# be sure args are handled properly
|
|
|
|
s = StringIO.StringIO()
|
|
|
|
sys.stdout = sys.stderr = s
|
|
|
|
save_handlers = atexit._exithandlers
|
|
|
|
atexit._exithandlers = []
|
|
|
|
try:
|
|
|
|
atexit.register(self.h1)
|
|
|
|
atexit.register(self.h4)
|
|
|
|
atexit.register(self.h4, 4, kw="abc")
|
|
|
|
atexit._run_exitfuncs()
|
|
|
|
finally:
|
|
|
|
sys.stdout = sys.__stdout__
|
|
|
|
sys.stderr = sys.__stderr__
|
|
|
|
atexit._exithandlers = save_handlers
|
|
|
|
self.assertEqual(s.getvalue(), "h4 (4,) {'kw': 'abc'}\nh4 () {}\nh1\n")
|
2000-06-28 12:07:31 -03:00
|
|
|
|
2004-11-04 00:31:30 -04:00
|
|
|
def test_order(self):
|
|
|
|
# be sure handlers are executed in reverse order
|
|
|
|
s = StringIO.StringIO()
|
|
|
|
sys.stdout = sys.stderr = s
|
|
|
|
save_handlers = atexit._exithandlers
|
|
|
|
atexit._exithandlers = []
|
|
|
|
try:
|
|
|
|
atexit.register(self.h1)
|
|
|
|
atexit.register(self.h2)
|
|
|
|
atexit.register(self.h3)
|
|
|
|
atexit._run_exitfuncs()
|
|
|
|
finally:
|
|
|
|
sys.stdout = sys.__stdout__
|
|
|
|
sys.stderr = sys.__stderr__
|
|
|
|
atexit._exithandlers = save_handlers
|
|
|
|
self.assertEqual(s.getvalue(), "h3\nh2\nh1\n")
|
2000-06-28 12:07:31 -03:00
|
|
|
|
2004-11-04 00:31:30 -04:00
|
|
|
def test_sys_override(self):
|
|
|
|
# be sure a preset sys.exitfunc is handled properly
|
|
|
|
s = StringIO.StringIO()
|
|
|
|
sys.stdout = sys.stderr = s
|
|
|
|
save_handlers = atexit._exithandlers
|
|
|
|
atexit._exithandlers = []
|
|
|
|
exfunc = sys.exitfunc
|
|
|
|
sys.exitfunc = self.h1
|
|
|
|
reload(atexit)
|
|
|
|
try:
|
|
|
|
atexit.register(self.h2)
|
|
|
|
atexit._run_exitfuncs()
|
|
|
|
finally:
|
|
|
|
sys.stdout = sys.__stdout__
|
|
|
|
sys.stderr = sys.__stderr__
|
|
|
|
atexit._exithandlers = save_handlers
|
|
|
|
sys.exitfunc = exfunc
|
|
|
|
self.assertEqual(s.getvalue(), "h2\nh1\n")
|
2002-07-16 16:30:59 -03:00
|
|
|
|
2004-11-04 00:31:30 -04:00
|
|
|
def test_raise(self):
|
|
|
|
# be sure raises are handled properly
|
|
|
|
s = StringIO.StringIO()
|
|
|
|
sys.stdout = sys.stderr = s
|
|
|
|
save_handlers = atexit._exithandlers
|
|
|
|
atexit._exithandlers = []
|
|
|
|
try:
|
|
|
|
atexit.register(self.raise1)
|
|
|
|
atexit.register(self.raise2)
|
|
|
|
self.assertRaises(TypeError, atexit._run_exitfuncs)
|
|
|
|
finally:
|
|
|
|
sys.stdout = sys.__stdout__
|
|
|
|
sys.stderr = sys.__stderr__
|
|
|
|
atexit._exithandlers = save_handlers
|
2004-11-07 00:52:29 -04:00
|
|
|
|
2004-11-04 00:31:30 -04:00
|
|
|
### helpers
|
|
|
|
def h1(self):
|
|
|
|
print "h1"
|
2002-07-16 16:30:59 -03:00
|
|
|
|
2004-11-04 00:31:30 -04:00
|
|
|
def h2(self):
|
|
|
|
print "h2"
|
2002-07-16 16:30:59 -03:00
|
|
|
|
2004-11-04 00:31:30 -04:00
|
|
|
def h3(self):
|
|
|
|
print "h3"
|
2002-07-16 16:30:59 -03:00
|
|
|
|
2004-11-04 00:31:30 -04:00
|
|
|
def h4(self, *args, **kwargs):
|
|
|
|
print "h4", args, kwargs
|
2002-07-16 16:30:59 -03:00
|
|
|
|
2004-11-04 00:31:30 -04:00
|
|
|
def raise1(self):
|
|
|
|
raise TypeError
|
2002-07-16 16:30:59 -03:00
|
|
|
|
2004-11-04 00:31:30 -04:00
|
|
|
def raise2(self):
|
|
|
|
raise SystemError
|
2002-07-16 16:30:59 -03:00
|
|
|
|
2004-11-04 00:31:30 -04:00
|
|
|
def test_main():
|
|
|
|
test_support.run_unittest(TestCase)
|
2000-06-28 12:07:31 -03:00
|
|
|
|
|
|
|
|
2004-11-04 00:31:30 -04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|