1 | n/a | import gc |
---|
2 | n/a | import io |
---|
3 | n/a | import os |
---|
4 | n/a | import sys |
---|
5 | n/a | import signal |
---|
6 | n/a | import weakref |
---|
7 | n/a | |
---|
8 | n/a | import unittest |
---|
9 | n/a | |
---|
10 | n/a | |
---|
11 | n/a | @unittest.skipUnless(hasattr(os, 'kill'), "Test requires os.kill") |
---|
12 | n/a | @unittest.skipIf(sys.platform =="win32", "Test cannot run on Windows") |
---|
13 | n/a | @unittest.skipIf(sys.platform == 'freebsd6', "Test kills regrtest on freebsd6 " |
---|
14 | n/a | "if threads have been used") |
---|
15 | n/a | class TestBreak(unittest.TestCase): |
---|
16 | n/a | int_handler = None |
---|
17 | n/a | |
---|
18 | n/a | def setUp(self): |
---|
19 | n/a | self._default_handler = signal.getsignal(signal.SIGINT) |
---|
20 | n/a | if self.int_handler is not None: |
---|
21 | n/a | signal.signal(signal.SIGINT, self.int_handler) |
---|
22 | n/a | |
---|
23 | n/a | def tearDown(self): |
---|
24 | n/a | signal.signal(signal.SIGINT, self._default_handler) |
---|
25 | n/a | unittest.signals._results = weakref.WeakKeyDictionary() |
---|
26 | n/a | unittest.signals._interrupt_handler = None |
---|
27 | n/a | |
---|
28 | n/a | |
---|
29 | n/a | def testInstallHandler(self): |
---|
30 | n/a | default_handler = signal.getsignal(signal.SIGINT) |
---|
31 | n/a | unittest.installHandler() |
---|
32 | n/a | self.assertNotEqual(signal.getsignal(signal.SIGINT), default_handler) |
---|
33 | n/a | |
---|
34 | n/a | try: |
---|
35 | n/a | pid = os.getpid() |
---|
36 | n/a | os.kill(pid, signal.SIGINT) |
---|
37 | n/a | except KeyboardInterrupt: |
---|
38 | n/a | self.fail("KeyboardInterrupt not handled") |
---|
39 | n/a | |
---|
40 | n/a | self.assertTrue(unittest.signals._interrupt_handler.called) |
---|
41 | n/a | |
---|
42 | n/a | def testRegisterResult(self): |
---|
43 | n/a | result = unittest.TestResult() |
---|
44 | n/a | unittest.registerResult(result) |
---|
45 | n/a | |
---|
46 | n/a | for ref in unittest.signals._results: |
---|
47 | n/a | if ref is result: |
---|
48 | n/a | break |
---|
49 | n/a | elif ref is not result: |
---|
50 | n/a | self.fail("odd object in result set") |
---|
51 | n/a | else: |
---|
52 | n/a | self.fail("result not found") |
---|
53 | n/a | |
---|
54 | n/a | |
---|
55 | n/a | def testInterruptCaught(self): |
---|
56 | n/a | default_handler = signal.getsignal(signal.SIGINT) |
---|
57 | n/a | |
---|
58 | n/a | result = unittest.TestResult() |
---|
59 | n/a | unittest.installHandler() |
---|
60 | n/a | unittest.registerResult(result) |
---|
61 | n/a | |
---|
62 | n/a | self.assertNotEqual(signal.getsignal(signal.SIGINT), default_handler) |
---|
63 | n/a | |
---|
64 | n/a | def test(result): |
---|
65 | n/a | pid = os.getpid() |
---|
66 | n/a | os.kill(pid, signal.SIGINT) |
---|
67 | n/a | result.breakCaught = True |
---|
68 | n/a | self.assertTrue(result.shouldStop) |
---|
69 | n/a | |
---|
70 | n/a | try: |
---|
71 | n/a | test(result) |
---|
72 | n/a | except KeyboardInterrupt: |
---|
73 | n/a | self.fail("KeyboardInterrupt not handled") |
---|
74 | n/a | self.assertTrue(result.breakCaught) |
---|
75 | n/a | |
---|
76 | n/a | |
---|
77 | n/a | def testSecondInterrupt(self): |
---|
78 | n/a | # Can't use skipIf decorator because the signal handler may have |
---|
79 | n/a | # been changed after defining this method. |
---|
80 | n/a | if signal.getsignal(signal.SIGINT) == signal.SIG_IGN: |
---|
81 | n/a | self.skipTest("test requires SIGINT to not be ignored") |
---|
82 | n/a | result = unittest.TestResult() |
---|
83 | n/a | unittest.installHandler() |
---|
84 | n/a | unittest.registerResult(result) |
---|
85 | n/a | |
---|
86 | n/a | def test(result): |
---|
87 | n/a | pid = os.getpid() |
---|
88 | n/a | os.kill(pid, signal.SIGINT) |
---|
89 | n/a | result.breakCaught = True |
---|
90 | n/a | self.assertTrue(result.shouldStop) |
---|
91 | n/a | os.kill(pid, signal.SIGINT) |
---|
92 | n/a | self.fail("Second KeyboardInterrupt not raised") |
---|
93 | n/a | |
---|
94 | n/a | try: |
---|
95 | n/a | test(result) |
---|
96 | n/a | except KeyboardInterrupt: |
---|
97 | n/a | pass |
---|
98 | n/a | else: |
---|
99 | n/a | self.fail("Second KeyboardInterrupt not raised") |
---|
100 | n/a | self.assertTrue(result.breakCaught) |
---|
101 | n/a | |
---|
102 | n/a | |
---|
103 | n/a | def testTwoResults(self): |
---|
104 | n/a | unittest.installHandler() |
---|
105 | n/a | |
---|
106 | n/a | result = unittest.TestResult() |
---|
107 | n/a | unittest.registerResult(result) |
---|
108 | n/a | new_handler = signal.getsignal(signal.SIGINT) |
---|
109 | n/a | |
---|
110 | n/a | result2 = unittest.TestResult() |
---|
111 | n/a | unittest.registerResult(result2) |
---|
112 | n/a | self.assertEqual(signal.getsignal(signal.SIGINT), new_handler) |
---|
113 | n/a | |
---|
114 | n/a | result3 = unittest.TestResult() |
---|
115 | n/a | |
---|
116 | n/a | def test(result): |
---|
117 | n/a | pid = os.getpid() |
---|
118 | n/a | os.kill(pid, signal.SIGINT) |
---|
119 | n/a | |
---|
120 | n/a | try: |
---|
121 | n/a | test(result) |
---|
122 | n/a | except KeyboardInterrupt: |
---|
123 | n/a | self.fail("KeyboardInterrupt not handled") |
---|
124 | n/a | |
---|
125 | n/a | self.assertTrue(result.shouldStop) |
---|
126 | n/a | self.assertTrue(result2.shouldStop) |
---|
127 | n/a | self.assertFalse(result3.shouldStop) |
---|
128 | n/a | |
---|
129 | n/a | |
---|
130 | n/a | def testHandlerReplacedButCalled(self): |
---|
131 | n/a | # Can't use skipIf decorator because the signal handler may have |
---|
132 | n/a | # been changed after defining this method. |
---|
133 | n/a | if signal.getsignal(signal.SIGINT) == signal.SIG_IGN: |
---|
134 | n/a | self.skipTest("test requires SIGINT to not be ignored") |
---|
135 | n/a | # If our handler has been replaced (is no longer installed) but is |
---|
136 | n/a | # called by the *new* handler, then it isn't safe to delay the |
---|
137 | n/a | # SIGINT and we should immediately delegate to the default handler |
---|
138 | n/a | unittest.installHandler() |
---|
139 | n/a | |
---|
140 | n/a | handler = signal.getsignal(signal.SIGINT) |
---|
141 | n/a | def new_handler(frame, signum): |
---|
142 | n/a | handler(frame, signum) |
---|
143 | n/a | signal.signal(signal.SIGINT, new_handler) |
---|
144 | n/a | |
---|
145 | n/a | try: |
---|
146 | n/a | pid = os.getpid() |
---|
147 | n/a | os.kill(pid, signal.SIGINT) |
---|
148 | n/a | except KeyboardInterrupt: |
---|
149 | n/a | pass |
---|
150 | n/a | else: |
---|
151 | n/a | self.fail("replaced but delegated handler doesn't raise interrupt") |
---|
152 | n/a | |
---|
153 | n/a | def testRunner(self): |
---|
154 | n/a | # Creating a TextTestRunner with the appropriate argument should |
---|
155 | n/a | # register the TextTestResult it creates |
---|
156 | n/a | runner = unittest.TextTestRunner(stream=io.StringIO()) |
---|
157 | n/a | |
---|
158 | n/a | result = runner.run(unittest.TestSuite()) |
---|
159 | n/a | self.assertIn(result, unittest.signals._results) |
---|
160 | n/a | |
---|
161 | n/a | def testWeakReferences(self): |
---|
162 | n/a | # Calling registerResult on a result should not keep it alive |
---|
163 | n/a | result = unittest.TestResult() |
---|
164 | n/a | unittest.registerResult(result) |
---|
165 | n/a | |
---|
166 | n/a | ref = weakref.ref(result) |
---|
167 | n/a | del result |
---|
168 | n/a | |
---|
169 | n/a | # For non-reference counting implementations |
---|
170 | n/a | gc.collect();gc.collect() |
---|
171 | n/a | self.assertIsNone(ref()) |
---|
172 | n/a | |
---|
173 | n/a | |
---|
174 | n/a | def testRemoveResult(self): |
---|
175 | n/a | result = unittest.TestResult() |
---|
176 | n/a | unittest.registerResult(result) |
---|
177 | n/a | |
---|
178 | n/a | unittest.installHandler() |
---|
179 | n/a | self.assertTrue(unittest.removeResult(result)) |
---|
180 | n/a | |
---|
181 | n/a | # Should this raise an error instead? |
---|
182 | n/a | self.assertFalse(unittest.removeResult(unittest.TestResult())) |
---|
183 | n/a | |
---|
184 | n/a | try: |
---|
185 | n/a | pid = os.getpid() |
---|
186 | n/a | os.kill(pid, signal.SIGINT) |
---|
187 | n/a | except KeyboardInterrupt: |
---|
188 | n/a | pass |
---|
189 | n/a | |
---|
190 | n/a | self.assertFalse(result.shouldStop) |
---|
191 | n/a | |
---|
192 | n/a | def testMainInstallsHandler(self): |
---|
193 | n/a | failfast = object() |
---|
194 | n/a | test = object() |
---|
195 | n/a | verbosity = object() |
---|
196 | n/a | result = object() |
---|
197 | n/a | default_handler = signal.getsignal(signal.SIGINT) |
---|
198 | n/a | |
---|
199 | n/a | class FakeRunner(object): |
---|
200 | n/a | initArgs = [] |
---|
201 | n/a | runArgs = [] |
---|
202 | n/a | def __init__(self, *args, **kwargs): |
---|
203 | n/a | self.initArgs.append((args, kwargs)) |
---|
204 | n/a | def run(self, test): |
---|
205 | n/a | self.runArgs.append(test) |
---|
206 | n/a | return result |
---|
207 | n/a | |
---|
208 | n/a | class Program(unittest.TestProgram): |
---|
209 | n/a | def __init__(self, catchbreak): |
---|
210 | n/a | self.exit = False |
---|
211 | n/a | self.verbosity = verbosity |
---|
212 | n/a | self.failfast = failfast |
---|
213 | n/a | self.catchbreak = catchbreak |
---|
214 | n/a | self.tb_locals = False |
---|
215 | n/a | self.testRunner = FakeRunner |
---|
216 | n/a | self.test = test |
---|
217 | n/a | self.result = None |
---|
218 | n/a | |
---|
219 | n/a | p = Program(False) |
---|
220 | n/a | p.runTests() |
---|
221 | n/a | |
---|
222 | n/a | self.assertEqual(FakeRunner.initArgs, [((), {'buffer': None, |
---|
223 | n/a | 'verbosity': verbosity, |
---|
224 | n/a | 'failfast': failfast, |
---|
225 | n/a | 'tb_locals': False, |
---|
226 | n/a | 'warnings': None})]) |
---|
227 | n/a | self.assertEqual(FakeRunner.runArgs, [test]) |
---|
228 | n/a | self.assertEqual(p.result, result) |
---|
229 | n/a | |
---|
230 | n/a | self.assertEqual(signal.getsignal(signal.SIGINT), default_handler) |
---|
231 | n/a | |
---|
232 | n/a | FakeRunner.initArgs = [] |
---|
233 | n/a | FakeRunner.runArgs = [] |
---|
234 | n/a | p = Program(True) |
---|
235 | n/a | p.runTests() |
---|
236 | n/a | |
---|
237 | n/a | self.assertEqual(FakeRunner.initArgs, [((), {'buffer': None, |
---|
238 | n/a | 'verbosity': verbosity, |
---|
239 | n/a | 'failfast': failfast, |
---|
240 | n/a | 'tb_locals': False, |
---|
241 | n/a | 'warnings': None})]) |
---|
242 | n/a | self.assertEqual(FakeRunner.runArgs, [test]) |
---|
243 | n/a | self.assertEqual(p.result, result) |
---|
244 | n/a | |
---|
245 | n/a | self.assertNotEqual(signal.getsignal(signal.SIGINT), default_handler) |
---|
246 | n/a | |
---|
247 | n/a | def testRemoveHandler(self): |
---|
248 | n/a | default_handler = signal.getsignal(signal.SIGINT) |
---|
249 | n/a | unittest.installHandler() |
---|
250 | n/a | unittest.removeHandler() |
---|
251 | n/a | self.assertEqual(signal.getsignal(signal.SIGINT), default_handler) |
---|
252 | n/a | |
---|
253 | n/a | # check that calling removeHandler multiple times has no ill-effect |
---|
254 | n/a | unittest.removeHandler() |
---|
255 | n/a | self.assertEqual(signal.getsignal(signal.SIGINT), default_handler) |
---|
256 | n/a | |
---|
257 | n/a | def testRemoveHandlerAsDecorator(self): |
---|
258 | n/a | default_handler = signal.getsignal(signal.SIGINT) |
---|
259 | n/a | unittest.installHandler() |
---|
260 | n/a | |
---|
261 | n/a | @unittest.removeHandler |
---|
262 | n/a | def test(): |
---|
263 | n/a | self.assertEqual(signal.getsignal(signal.SIGINT), default_handler) |
---|
264 | n/a | |
---|
265 | n/a | test() |
---|
266 | n/a | self.assertNotEqual(signal.getsignal(signal.SIGINT), default_handler) |
---|
267 | n/a | |
---|
268 | n/a | @unittest.skipUnless(hasattr(os, 'kill'), "Test requires os.kill") |
---|
269 | n/a | @unittest.skipIf(sys.platform =="win32", "Test cannot run on Windows") |
---|
270 | n/a | @unittest.skipIf(sys.platform == 'freebsd6', "Test kills regrtest on freebsd6 " |
---|
271 | n/a | "if threads have been used") |
---|
272 | n/a | class TestBreakDefaultIntHandler(TestBreak): |
---|
273 | n/a | int_handler = signal.default_int_handler |
---|
274 | n/a | |
---|
275 | n/a | @unittest.skipUnless(hasattr(os, 'kill'), "Test requires os.kill") |
---|
276 | n/a | @unittest.skipIf(sys.platform =="win32", "Test cannot run on Windows") |
---|
277 | n/a | @unittest.skipIf(sys.platform == 'freebsd6', "Test kills regrtest on freebsd6 " |
---|
278 | n/a | "if threads have been used") |
---|
279 | n/a | class TestBreakSignalIgnored(TestBreak): |
---|
280 | n/a | int_handler = signal.SIG_IGN |
---|
281 | n/a | |
---|
282 | n/a | @unittest.skipUnless(hasattr(os, 'kill'), "Test requires os.kill") |
---|
283 | n/a | @unittest.skipIf(sys.platform =="win32", "Test cannot run on Windows") |
---|
284 | n/a | @unittest.skipIf(sys.platform == 'freebsd6', "Test kills regrtest on freebsd6 " |
---|
285 | n/a | "if threads have been used") |
---|
286 | n/a | class TestBreakSignalDefault(TestBreak): |
---|
287 | n/a | int_handler = signal.SIG_DFL |
---|
288 | n/a | |
---|
289 | n/a | |
---|
290 | n/a | if __name__ == "__main__": |
---|
291 | n/a | unittest.main() |
---|