(firstlines, message, charset, lineno)
| 426 | output_encoding = str(stdout, 'ascii').splitlines()[0] |
| 427 | |
| 428 | def do_test(firstlines, message, charset, lineno): |
| 429 | # Raise the message in a subprocess, and catch the output |
| 430 | try: |
| 431 | with open(TESTFN, "w", encoding=charset) as output: |
| 432 | output.write("""{0}if 1: |
| 433 | import traceback; |
| 434 | raise RuntimeError('{1}') |
| 435 | """.format(firstlines, message)) |
| 436 | |
| 437 | process = subprocess.Popen([sys.executable, TESTFN], |
| 438 | stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 439 | stdout, stderr = process.communicate() |
| 440 | stdout = stdout.decode(output_encoding).splitlines() |
| 441 | finally: |
| 442 | unlink(TESTFN) |
| 443 | |
| 444 | # The source lines are encoded with the 'backslashreplace' handler |
| 445 | encoded_message = message.encode(output_encoding, |
| 446 | 'backslashreplace') |
| 447 | # and we just decoded them with the output_encoding. |
| 448 | message_ascii = encoded_message.decode(output_encoding) |
| 449 | |
| 450 | err_line = "raise RuntimeError('{0}')".format(message_ascii) |
| 451 | err_msg = "RuntimeError: {0}".format(message_ascii) |
| 452 | |
| 453 | self.assertIn("line %s" % lineno, stdout[1]) |
| 454 | self.assertEndsWith(stdout[2], err_line) |
| 455 | actual_err_msg = stdout[3] |
| 456 | self.assertEqual(actual_err_msg, err_msg) |
| 457 | |
| 458 | do_test("", "foo", "ascii", 3) |
| 459 | for charset in ("ascii", "iso-8859-1", "utf-8", "GBK"): |
nothing calls this directly
no test coverage detected