Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(9)

Side by Side Diff: Lib/test/test_faulthandler.py

Issue 10639: reindent.py converts newlines to platform default
Patch Set: Created 8 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/test/test_extcall.py ('k') | Lib/test/test_fcntl.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 from contextlib import contextmanager 1 from contextlib import contextmanager
2 import datetime 2 import datetime
3 import faulthandler 3 import faulthandler
4 import os
5 import re 4 import re
6 import signal 5 import signal
7 import subprocess 6 import subprocess
8 import sys 7 import sys
9 from test import support, script_helper 8 from test import support, script_helper
10 import tempfile 9 import tempfile
11 import unittest 10 import unittest
12 11
13 try: 12 try:
14 import threading 13 import threading
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 faulthandler._read_null() 223 faulthandler._read_null()
225 """.strip() 224 """.strip()
226 not_expected = 'Fatal Python error' 225 not_expected = 'Fatal Python error'
227 stderr, exitcode = self.get_output(code) 226 stderr, exitcode = self.get_output(code)
228 stder = '\n'.join(stderr) 227 stder = '\n'.join(stderr)
229 self.assertTrue(not_expected not in stderr, 228 self.assertTrue(not_expected not in stderr,
230 "%r is present in %r" % (not_expected, stderr)) 229 "%r is present in %r" % (not_expected, stderr))
231 self.assertNotEqual(exitcode, 0) 230 self.assertNotEqual(exitcode, 0)
232 231
233 def test_is_enabled(self): 232 def test_is_enabled(self):
234 orig_stderr = sys.stderr 233 was_enabled = faulthandler.is_enabled()
235 try: 234 try:
236 # regrtest may replace sys.stderr by io.StringIO object, but 235 faulthandler.enable()
237 # faulthandler.enable() requires that sys.stderr has a fileno() 236 self.assertTrue(faulthandler.is_enabled())
238 # method 237 faulthandler.disable()
239 sys.stderr = sys.__stderr__ 238 self.assertFalse(faulthandler.is_enabled())
240 239 finally:
241 was_enabled = faulthandler.is_enabled() 240 if was_enabled:
242 try:
243 faulthandler.enable() 241 faulthandler.enable()
244 self.assertTrue(faulthandler.is_enabled()) 242 else:
245 faulthandler.disable() 243 faulthandler.disable()
246 self.assertFalse(faulthandler.is_enabled())
247 finally:
248 if was_enabled:
249 faulthandler.enable()
250 else:
251 faulthandler.disable()
252 finally:
253 sys.stderr = orig_stderr
254 244
255 def check_dump_traceback(self, filename): 245 def check_dump_traceback(self, filename):
256 """ 246 """
257 Explicitly call dump_traceback() function and check its output. 247 Explicitly call dump_traceback() function and check its output.
258 Raise an error if the output doesn't match the expected format. 248 Raise an error if the output doesn't match the expected format.
259 """ 249 """
260 code = """ 250 code = """
261 import faulthandler 251 import faulthandler
262 252
263 def funcB(): 253 def funcB():
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
445 435
446 def test_dump_tracebacks_later_file(self): 436 def test_dump_tracebacks_later_file(self):
447 self.check_dump_tracebacks_later(file=True) 437 self.check_dump_tracebacks_later(file=True)
448 438
449 def test_dump_tracebacks_later_twice(self): 439 def test_dump_tracebacks_later_twice(self):
450 self.check_dump_tracebacks_later(twice=True) 440 self.check_dump_tracebacks_later(twice=True)
451 441
452 @unittest.skipIf(not hasattr(faulthandler, "register"), 442 @unittest.skipIf(not hasattr(faulthandler, "register"),
453 "need faulthandler.register") 443 "need faulthandler.register")
454 def check_register(self, filename=False, all_threads=False, 444 def check_register(self, filename=False, all_threads=False,
455 unregister=False, chain=False): 445 unregister=False):
456 """ 446 """
457 Register a handler displaying the traceback on a user signal. Raise the 447 Register a handler displaying the traceback on a user signal. Raise the
458 signal and check the written traceback. 448 signal and check the written traceback.
459 449
460 If chain is True, check that the previous signal handler is called.
461
462 Raise an error if the output doesn't match the expected format. 450 Raise an error if the output doesn't match the expected format.
463 """ 451 """
464 signum = signal.SIGUSR1 452 signum = signal.SIGUSR1
465 code = """ 453 code = """
466 import faulthandler 454 import faulthandler
467 import os 455 import os
468 import signal 456 import signal
469 import sys
470 457
471 def func(signum): 458 def func(signum):
472 os.kill(os.getpid(), signum) 459 os.kill(os.getpid(), signum)
473 460
474 def handler(signum, frame):
475 handler.called = True
476 handler.called = False
477
478 exitcode = 0
479 signum = {signum} 461 signum = {signum}
480 unregister = {unregister} 462 unregister = {unregister}
481 chain = {chain}
482
483 if {has_filename}: 463 if {has_filename}:
484 file = open({filename}, "wb") 464 file = open({filename}, "wb")
485 else: 465 else:
486 file = None 466 file = None
487 if chain: 467 faulthandler.register(signum, file=file, all_threads={all_threads})
488 signal.signal(signum, handler)
489 faulthandler.register(signum, file=file,
490 all_threads={all_threads}, chain={chain})
491 if unregister: 468 if unregister:
492 faulthandler.unregister(signum) 469 faulthandler.unregister(signum)
493 func(signum) 470 func(signum)
494 if chain and not handler.called:
495 if file is not None:
496 output = file
497 else:
498 output = sys.stderr
499 print("Error: signal handler not called!", file=output)
500 exitcode = 1
501 if file is not None: 471 if file is not None:
502 file.close() 472 file.close()
503 sys.exit(exitcode)
504 """.strip() 473 """.strip()
505 code = code.format( 474 code = code.format(
506 filename=repr(filename), 475 filename=repr(filename),
507 has_filename=bool(filename), 476 has_filename=bool(filename),
508 all_threads=all_threads, 477 all_threads=all_threads,
509 signum=signum, 478 signum=signum,
510 unregister=unregister, 479 unregister=unregister,
511 chain=chain,
512 ) 480 )
513 trace, exitcode = self.get_output(code, filename) 481 trace, exitcode = self.get_output(code, filename)
514 trace = '\n'.join(trace) 482 trace = '\n'.join(trace)
515 if not unregister: 483 if not unregister:
516 if all_threads: 484 if all_threads:
517 regex = 'Current thread XXX:\n' 485 regex = 'Current thread XXX:\n'
518 else: 486 else:
519 regex = 'Traceback \(most recent call first\):\n' 487 regex = 'Traceback \(most recent call first\):\n'
520 regex = expected_traceback(7, 28, regex) 488 regex = expected_traceback(6, 17, regex)
521 self.assertRegex(trace, regex) 489 self.assertRegex(trace, regex)
522 else: 490 else:
523 self.assertEqual(trace, '') 491 self.assertEqual(trace, '')
524 if unregister: 492 if unregister:
525 self.assertNotEqual(exitcode, 0) 493 self.assertNotEqual(exitcode, 0)
526 else: 494 else:
527 self.assertEqual(exitcode, 0) 495 self.assertEqual(exitcode, 0)
528 496
529 def test_register(self): 497 def test_register(self):
530 self.check_register() 498 self.check_register()
531 499
532 def test_unregister(self): 500 def test_unregister(self):
533 self.check_register(unregister=True) 501 self.check_register(unregister=True)
534 502
535 def test_register_file(self): 503 def test_register_file(self):
536 with temporary_filename() as filename: 504 with temporary_filename() as filename:
537 self.check_register(filename=filename) 505 self.check_register(filename=filename)
538 506
539 def test_register_threads(self): 507 def test_register_threads(self):
540 self.check_register(all_threads=True) 508 self.check_register(all_threads=True)
541
542 def test_register_chain(self):
543 self.check_register(chain=True)
544 509
545 510
546 def test_main(): 511 def test_main():
547 support.run_unittest(FaultHandlerTests) 512 support.run_unittest(FaultHandlerTests)
548 513
549 if __name__ == "__main__": 514 if __name__ == "__main__":
550 test_main() 515 test_main()
OLDNEW
« no previous file with comments | « Lib/test/test_extcall.py ('k') | Lib/test/test_fcntl.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+