Issue #25677: Correct syntax error caret for indented blocks.

Based on patch by Michael Layzell.
This commit is contained in:
Martin Panter 2016-12-11 00:18:36 +00:00
parent b94eef2ae3
commit ca3263c50c
5 changed files with 39 additions and 5 deletions

View File

@ -10,6 +10,7 @@ import os
import os.path import os.path
import py_compile import py_compile
import subprocess import subprocess
import io
import textwrap import textwrap
from test import support from test import support
@ -540,6 +541,38 @@ class CmdLineTest(unittest.TestCase):
text = stderr.decode('ascii') text = stderr.decode('ascii')
self.assertEqual(text, "some text") self.assertEqual(text, "some text")
def test_syntaxerror_unindented_caret_position(self):
script = "1 + 1 = 2\n"
with support.temp_dir() as script_dir:
script_name = _make_test_script(script_dir, 'script', script)
exitcode, stdout, stderr = assert_python_failure(script_name)
text = io.TextIOWrapper(io.BytesIO(stderr), 'ascii').read()
# Confirm that the caret is located under the first 1 character
self.assertIn("\n 1 + 1 = 2\n ^", text)
def test_syntaxerror_indented_caret_position(self):
script = textwrap.dedent("""\
if True:
1 + 1 = 2
""")
with support.temp_dir() as script_dir:
script_name = _make_test_script(script_dir, 'script', script)
exitcode, stdout, stderr = assert_python_failure(script_name)
text = io.TextIOWrapper(io.BytesIO(stderr), 'ascii').read()
# Confirm that the caret is located under the first 1 character
self.assertIn("\n 1 + 1 = 2\n ^", text)
# Try the same with a form feed at the start of the indented line
script = (
"if True:\n"
"\f 1 + 1 = 2\n"
)
script_name = _make_test_script(script_dir, "script", script)
exitcode, stdout, stderr = assert_python_failure(script_name)
text = io.TextIOWrapper(io.BytesIO(stderr), "ascii").read()
self.assertNotIn("\f", text)
self.assertIn("\n 1 + 1 = 2\n ^", text)
def test_main(): def test_main():
support.run_unittest(CmdLineTest) support.run_unittest(CmdLineTest)

View File

@ -840,6 +840,7 @@ Julia Lawall
Chris Lawrence Chris Lawrence
Mark Lawrence Mark Lawrence
Chris Laws Chris Laws
Michael Layzell
Michael Lazar Michael Lazar
Brian Leair Brian Leair
Mathieu Leduc-Hamel Mathieu Leduc-Hamel

View File

@ -10,6 +10,9 @@ Release date: TBA
Core and Builtins Core and Builtins
----------------- -----------------
- Issue #25677: Correct the positioning of the syntax error caret for
indented blocks. Based on patch by Michael Layzell.
- Issue #29000: Fixed bytes formatting of octals with zero padding in alternate - Issue #29000: Fixed bytes formatting of octals with zero padding in alternate
form. form.

View File

@ -1094,11 +1094,8 @@ err_programtext(FILE *fp, int lineno)
} }
fclose(fp); fclose(fp);
if (i == lineno) { if (i == lineno) {
char *p = linebuf;
PyObject *res; PyObject *res;
while (*p == ' ' || *p == '\t' || *p == '\014') res = PyUnicode_FromString(linebuf);
p++;
res = PyUnicode_FromString(p);
if (res == NULL) if (res == NULL)
PyErr_Clear(); PyErr_Clear();
return res; return res;

View File

@ -528,7 +528,7 @@ print_error_text(PyObject *f, int offset, PyObject *text_obj)
offset -= (int)(nl+1-text); offset -= (int)(nl+1-text);
text = nl+1; text = nl+1;
} }
while (*text == ' ' || *text == '\t') { while (*text == ' ' || *text == '\t' || *text == '\f') {
text++; text++;
offset--; offset--;
} }