|
10 | 10 | import os.path |
11 | 11 | import py_compile |
12 | 12 | import subprocess |
| 13 | +import io |
13 | 14 |
|
14 | 15 | import textwrap |
15 | 16 | from test import support |
@@ -539,6 +540,38 @@ def test_issue20500_exit_with_exception_value(self): |
539 | 540 | text = stderr.decode('ascii') |
540 | 541 | self.assertEqual(text, "some text") |
541 | 542 |
|
| 543 | + def test_syntaxerror_unindented_caret_position(self): |
| 544 | + script = "1 + 1 = 2\n" |
| 545 | + with support.temp_dir() as script_dir: |
| 546 | + script_name = _make_test_script(script_dir, 'script', script) |
| 547 | + exitcode, stdout, stderr = assert_python_failure(script_name) |
| 548 | + text = io.TextIOWrapper(io.BytesIO(stderr), 'ascii').read() |
| 549 | + # Confirm that the caret is located under the first 1 character |
| 550 | + self.assertIn("\n 1 + 1 = 2\n ^", text) |
| 551 | + |
| 552 | + def test_syntaxerror_indented_caret_position(self): |
| 553 | + script = textwrap.dedent("""\ |
| 554 | + if True: |
| 555 | + 1 + 1 = 2 |
| 556 | + """) |
| 557 | + with support.temp_dir() as script_dir: |
| 558 | + script_name = _make_test_script(script_dir, 'script', script) |
| 559 | + exitcode, stdout, stderr = assert_python_failure(script_name) |
| 560 | + text = io.TextIOWrapper(io.BytesIO(stderr), 'ascii').read() |
| 561 | + # Confirm that the caret is located under the first 1 character |
| 562 | + self.assertIn("\n 1 + 1 = 2\n ^", text) |
| 563 | + |
| 564 | + # Try the same with a form feed at the start of the indented line |
| 565 | + script = ( |
| 566 | + "if True:\n" |
| 567 | + "\f 1 + 1 = 2\n" |
| 568 | + ) |
| 569 | + script_name = _make_test_script(script_dir, "script", script) |
| 570 | + exitcode, stdout, stderr = assert_python_failure(script_name) |
| 571 | + text = io.TextIOWrapper(io.BytesIO(stderr), "ascii").read() |
| 572 | + self.assertNotIn("\f", text) |
| 573 | + self.assertIn("\n 1 + 1 = 2\n ^", text) |
| 574 | + |
542 | 575 |
|
543 | 576 | def test_main(): |
544 | 577 | support.run_unittest(CmdLineTest) |
|
0 commit comments