Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_atexit.py @ 54294:dba767bc3155 legacy-trunk
remove mention of old ctypes version
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Sat, 23 May 2009 20:59:09 +0000 |
| parents | ce7b8c317da2 |
| children | dc94134186c8 |
| rev | line source |
|---|---|
|
33896
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
1 import sys |
|
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
2 import unittest |
|
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
3 import StringIO |
|
24249
4a7b3bac3812
The atexit module effectively turned itself off if sys.exitfunc already
Tim Peters <tim.peters@gmail.com>
parents:
15966
diff
changeset
|
4 import atexit |
|
33896
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
5 from test import test_support |
|
13051
f13384105528
Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
6 |
|
33896
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
7 class TestCase(unittest.TestCase): |
| 49455 | 8 def setUp(self): |
|
33896
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
9 s = StringIO.StringIO() |
| 49463 | 10 self.save_stdout = sys.stdout |
| 11 self.save_stderr = sys.stderr | |
| 49455 | 12 sys.stdout = sys.stderr = self.subst_io = s |
| 13 self.save_handlers = atexit._exithandlers | |
|
33896
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
14 atexit._exithandlers = [] |
| 49455 | 15 |
| 16 def tearDown(self): | |
| 49463 | 17 sys.stdout = self.save_stdout |
| 18 sys.stderr = self.save_stderr | |
| 49455 | 19 atexit._exithandlers = self.save_handlers |
| 20 | |
| 21 def test_args(self): | |
| 22 atexit.register(self.h1) | |
| 23 atexit.register(self.h4) | |
| 24 atexit.register(self.h4, 4, kw="abc") | |
| 25 atexit._run_exitfuncs() | |
| 26 self.assertEqual(self.subst_io.getvalue(), | |
| 27 "h4 (4,) {'kw': 'abc'}\nh4 () {}\nh1\n") | |
|
13051
f13384105528
Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
28 |
|
49283
f8430b6876fb
backport the atexit test for r66563
Benjamin Peterson <benjamin@python.org>
parents:
45872
diff
changeset
|
29 def test_badargs(self): |
| 49455 | 30 atexit.register(lambda: 1, 0, 0, (x for x in (1,2)), 0, 0) |
| 31 self.assertRaises(TypeError, atexit._run_exitfuncs) | |
|
49283
f8430b6876fb
backport the atexit test for r66563
Benjamin Peterson <benjamin@python.org>
parents:
45872
diff
changeset
|
32 |
|
33896
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
33 def test_order(self): |
| 49455 | 34 atexit.register(self.h1) |
| 35 atexit.register(self.h2) | |
| 36 atexit.register(self.h3) | |
| 37 atexit._run_exitfuncs() | |
| 38 self.assertEqual(self.subst_io.getvalue(), "h3\nh2\nh1\n") | |
|
24249
4a7b3bac3812
The atexit module effectively turned itself off if sys.exitfunc already
Tim Peters <tim.peters@gmail.com>
parents:
15966
diff
changeset
|
39 |
|
33896
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
40 def test_sys_override(self): |
|
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
41 # be sure a preset sys.exitfunc is handled properly |
|
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
42 exfunc = sys.exitfunc |
|
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
43 sys.exitfunc = self.h1 |
|
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
44 reload(atexit) |
|
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
45 try: |
|
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
46 atexit.register(self.h2) |
|
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
47 atexit._run_exitfuncs() |
|
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
48 finally: |
|
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
49 sys.exitfunc = exfunc |
| 49455 | 50 self.assertEqual(self.subst_io.getvalue(), "h2\nh1\n") |
|
13051
f13384105528
Added the atexit module and documentation from Skip Montanaro
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
51 |
|
33896
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
52 def test_raise(self): |
| 49455 | 53 atexit.register(self.raise1) |
| 54 atexit.register(self.raise2) | |
| 55 self.assertRaises(TypeError, atexit._run_exitfuncs) | |
|
33920
d82fa932b41b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33896
diff
changeset
|
56 |
|
33896
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
57 ### helpers |
|
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
58 def h1(self): |
|
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
59 print "h1" |
|
24249
4a7b3bac3812
The atexit module effectively turned itself off if sys.exitfunc already
Tim Peters <tim.peters@gmail.com>
parents:
15966
diff
changeset
|
60 |
|
33896
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
61 def h2(self): |
|
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
62 print "h2" |
|
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
63 |
|
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
64 def h3(self): |
|
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
65 print "h3" |
|
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
66 |
|
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
67 def h4(self, *args, **kwargs): |
|
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
68 print "h4", args, kwargs |
|
24249
4a7b3bac3812
The atexit module effectively turned itself off if sys.exitfunc already
Tim Peters <tim.peters@gmail.com>
parents:
15966
diff
changeset
|
69 |
|
33896
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
70 def raise1(self): |
|
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
71 raise TypeError |
|
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
72 |
|
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
73 def raise2(self): |
|
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
74 raise SystemError |
|
24249
4a7b3bac3812
The atexit module effectively turned itself off if sys.exitfunc already
Tim Peters <tim.peters@gmail.com>
parents:
15966
diff
changeset
|
75 |
|
33896
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
76 def test_main(): |
|
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
77 test_support.run_unittest(TestCase) |
|
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
78 |
|
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
79 |
|
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
80 if __name__ == "__main__": |
|
710b7e8d2ff2
Fix bug 1052242. Also includes rewrite of test case using unittest and
Skip Montanaro <skip@pobox.com>
parents:
28048
diff
changeset
|
81 test_main() |
