summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_atexit.py
diff options
context:
space:
mode:
authorCollin Winter <collinw@gmail.com>2007-03-21 02:57:17 (GMT)
committerCollin Winter <collinw@gmail.com>2007-03-21 02:57:17 (GMT)
commit670e6921349dd408b6958a0c5d3b1486725f9beb (patch)
tree719d6b0e5348693a2d985dfabc5e9160f7f79108 /Lib/test/test_atexit.py
parent450ee81b227b093eeb0b7a933fe6ddc6dc768d4a (diff)
downloadcpython-670e6921349dd408b6958a0c5d3b1486725f9beb.zip
cpython-670e6921349dd408b6958a0c5d3b1486725f9beb.tar.gz
cpython-670e6921349dd408b6958a0c5d3b1486725f9beb.tar.bz2
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Diffstat (limited to 'Lib/test/test_atexit.py')
-rw-r--r--Lib/test/test_atexit.py173
1 files changed, 94 insertions, 79 deletions
diff --git a/Lib/test/test_atexit.py b/Lib/test/test_atexit.py
index 9d1e003..56077e7 100644
--- a/Lib/test/test_atexit.py
+++ b/Lib/test/test_atexit.py
@@ -4,97 +4,112 @@ import StringIO
import atexit
from test import test_support
-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")
+### helpers
+def h1():
+ print("h1")
- 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")
+def h2():
+ print("h2")
- 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")
+def h3():
+ print("h3")
- 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
+def h4(*args, **kwargs):
+ print("h4", args, kwargs)
+
+def raise1():
+ raise TypeError
- ### helpers
- def h1(self):
- print("h1")
+def raise2():
+ raise SystemError
- def h2(self):
- print("h2")
+class TestCase(unittest.TestCase):
+ def setUp(self):
+ self.stream = StringIO.StringIO()
+ sys.stdout = sys.stderr = self.stream
+ atexit._clear()
+
+ def tearDown(self):
+ sys.stdout = sys.__stdout__
+ sys.stderr = sys.__stderr__
+ atexit._clear()
- def h3(self):
- print("h3")
+ def test_args(self):
+ # be sure args are handled properly
+ atexit.register(h1)
+ atexit.register(h4)
+ atexit.register(h4, 4, kw="abc")
+ atexit._run_exitfuncs()
- def h4(self, *args, **kwargs):
- print("h4", args, kwargs)
+ self.assertEqual(self.stream.getvalue(),
+ "h4 (4,) {'kw': 'abc'}\nh4 () {}\nh1\n")
- def raise1(self):
- raise TypeError
+ def test_order(self):
+ # be sure handlers are executed in reverse order
+ atexit.register(h1)
+ atexit.register(h2)
+ atexit.register(h3)
+ atexit._run_exitfuncs()
+
+ self.assertEqual(self.stream.getvalue(), "h3\nh2\nh1\n")
- def raise2(self):
- raise SystemError
+ def test_raise(self):
+ # be sure raises are handled properly
+ atexit.register(raise1)
+ atexit.register(raise2)
+
+ self.assertRaises(TypeError, atexit._run_exitfuncs)
+
+ def test_stress(self):
+ a = [0]
+ def inc():
+ a[0] += 1
+
+ for i in range(128):
+ atexit.register(inc)
+ atexit._run_exitfuncs()
+
+ self.assertEqual(a[0], 128)
+
+ def test_clear(self):
+ a = [0]
+ def inc():
+ a[0] += 1
+
+ atexit.register(inc)
+ atexit._clear()
+ atexit._run_exitfuncs()
+
+ self.assertEqual(a[0], 0)
+
+ def test_unregister(self):
+ a = [0]
+ def inc():
+ a[0] += 1
+ def dec():
+ a[0] -= 1
+
+ for i in range(4):
+ atexit.register(inc)
+ atexit.register(dec)
+ atexit.unregister(inc)
+ atexit._run_exitfuncs()
+
+ self.assertEqual(a[0], -1)
+
+ def test_bound_methods(self):
+ l = []
+ atexit.register(l.append, 5)
+ atexit._run_exitfuncs()
+ self.assertEqual(l, [5])
+
+ atexit.unregister(l.append)
+ atexit._run_exitfuncs()
+ self.assertEqual(l, [5])
+
def test_main():
test_support.run_unittest(TestCase)
-
if __name__ == "__main__":
test_main()