Skip to content

Commit 619555d

Browse files
committed
Issue #25677: Merge SyntaxError caret positioning from 3.5
2 parents 879199b + ca3263c commit 619555d

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
@@ -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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,7 @@ Julia Lawall
851851
Chris Lawrence
852852
Mark Lawrence
853853
Chris Laws
854+
Michael Layzell
854855
Michael Lazar
855856
Brian Leair
856857
Mathieu Leduc-Hamel

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.6.1 release candidate 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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,11 +1144,8 @@ err_programtext(FILE *fp, int lineno)
11441144
}
11451145
fclose(fp);
11461146
if (i == lineno) {
1147-
char *p = linebuf;
11481147
PyObject *res;
1149-
while (*p == ' ' || *p == '\t' || *p == '\014')
1150-
p++;
1151-
res = PyUnicode_FromString(p);
1148+
res = PyUnicode_FromString(linebuf);
11521149
if (res == NULL)
11531150
PyErr_Clear();
11541151
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)