Skip to content

Commit a967fce

Browse files
committed
Issue #25677: Merge SyntaxError caret positioning from 3.6
2 parents 8b4924d + 1350efc commit a967fce

File tree

5 files changed

+39
-5
lines changed

5 files changed

+39
-5
lines changed

Lib/test/test_cmd_line_script.py

+33
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
@@ -539,6 +540,38 @@ def test_issue20500_exit_with_exception_value(self):
539540
text = stderr.decode('ascii')
540541
self.assertEqual(text, "some text")
541542

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+
542575

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

Misc/ACKS

+1
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,7 @@ Julia Lawall
852852
Chris Lawrence
853853
Mark Lawrence
854854
Chris Laws
855+
Michael Layzell
855856
Michael Lazar
856857
Brian Leair
857858
Mathieu Leduc-Hamel

Misc/NEWS

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.7.0 alpha 1
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

+1-4
Original file line numberDiff line numberDiff line change
@@ -1138,11 +1138,8 @@ err_programtext(FILE *fp, int lineno)
11381138
}
11391139
fclose(fp);
11401140
if (i == lineno) {
1141-
char *p = linebuf;
11421141
PyObject *res;
1143-
while (*p == ' ' || *p == '\t' || *p == '\014')
1144-
p++;
1145-
res = PyUnicode_FromString(p);
1142+
res = PyUnicode_FromString(linebuf);
11461143
if (res == NULL)
11471144
PyErr_Clear();
11481145
return res;

Python/pythonrun.c

+1-1
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)