Skip to content

Commit ca3263c

Browse files
committed
Issue #25677: Correct syntax error caret for indented blocks.
Based on patch by Michael Layzell.
1 parent b94eef2 commit ca3263c

File tree

5 files changed

+39
-5
lines changed

5 files changed

+39
-5
lines changed

Lib/test/test_cmd_line_script.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import os.path
1111
import py_compile
1212
import subprocess
13+
import io
1314

1415
import textwrap
1516
from test import support
@@ -540,6 +541,38 @@ def test_issue20500_exit_with_exception_value(self):
540541
text = stderr.decode('ascii')
541542
self.assertEqual(text, "some text")
542543

544+
def test_syntaxerror_unindented_caret_position(self):
545+
script = "1 + 1 = 2\n"
546+
with support.temp_dir() as script_dir:
547+
script_name = _make_test_script(script_dir, 'script', script)
548+
exitcode, stdout, stderr = assert_python_failure(script_name)
549+
text = io.TextIOWrapper(io.BytesIO(stderr), 'ascii').read()
550+
# Confirm that the caret is located under the first 1 character
551+
self.assertIn("\n 1 + 1 = 2\n ^", text)
552+
553+
def test_syntaxerror_indented_caret_position(self):
554+
script = textwrap.dedent("""\
555+
if True:
556+
1 + 1 = 2
557+
""")
558+
with support.temp_dir() as script_dir:
559+
script_name = _make_test_script(script_dir, 'script', script)
560+
exitcode, stdout, stderr = assert_python_failure(script_name)
561+
text = io.TextIOWrapper(io.BytesIO(stderr), 'ascii').read()
562+
# Confirm that the caret is located under the first 1 character
563+
self.assertIn("\n 1 + 1 = 2\n ^", text)
564+
565+
# Try the same with a form feed at the start of the indented line
566+
script = (
567+
"if True:\n"
568+
"\f 1 + 1 = 2\n"
569+
)
570+
script_name = _make_test_script(script_dir, "script", script)
571+
exitcode, stdout, stderr = assert_python_failure(script_name)
572+
text = io.TextIOWrapper(io.BytesIO(stderr), "ascii").read()
573+
self.assertNotIn("\f", text)
574+
self.assertIn("\n 1 + 1 = 2\n ^", text)
575+
543576

544577
def test_main():
545578
support.run_unittest(CmdLineTest)

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,7 @@ Julia Lawall
840840
Chris Lawrence
841841
Mark Lawrence
842842
Chris Laws
843+
Michael Layzell
843844
Michael Lazar
844845
Brian Leair
845846
Mathieu Leduc-Hamel

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Release date: TBA
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #25677: Correct the positioning of the syntax error caret for
14+
indented blocks. Based on patch by Michael Layzell.
15+
1316
- Issue #29000: Fixed bytes formatting of octals with zero padding in alternate
1417
form.
1518

Python/errors.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,11 +1094,8 @@ err_programtext(FILE *fp, int lineno)
10941094
}
10951095
fclose(fp);
10961096
if (i == lineno) {
1097-
char *p = linebuf;
10981097
PyObject *res;
1099-
while (*p == ' ' || *p == '\t' || *p == '\014')
1100-
p++;
1101-
res = PyUnicode_FromString(p);
1098+
res = PyUnicode_FromString(linebuf);
11021099
if (res == NULL)
11031100
PyErr_Clear();
11041101
return res;

Python/pythonrun.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ print_error_text(PyObject *f, int offset, PyObject *text_obj)
528528
offset -= (int)(nl+1-text);
529529
text = nl+1;
530530
}
531-
while (*text == ' ' || *text == '\t') {
531+
while (*text == ' ' || *text == '\t' || *text == '\f') {
532532
text++;
533533
offset--;
534534
}

0 commit comments

Comments
 (0)