From 2d778c23fa45e4c92fabaf7395ac85174884e4fc Mon Sep 17 00:00:00 2001 From: Michael Layzell Date: Fri, 20 Nov 2015 15:13:23 -0500 Subject: [PATCH] Issue #25677: Correct syntax error caret for indented blocks. --- Lib/test/test_cmd_line_script.py | 22 ++++++++++++++++++++++ Python/errors.c | 5 +---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_cmd_line_script.py b/Lib/test/test_cmd_line_script.py index fda3e62..68dd10d 100644 --- a/Lib/test/test_cmd_line_script.py +++ b/Lib/test/test_cmd_line_script.py @@ -10,6 +10,7 @@ import os import os.path import py_compile import subprocess +import io import textwrap from test import support @@ -471,6 +472,27 @@ class CmdLineTest(unittest.TestCase): text = stderr.decode('ascii') 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) + def test_main(): support.run_unittest(CmdLineTest) diff --git a/Python/errors.c b/Python/errors.c index aed2bdc..778f8f2 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -1096,11 +1096,8 @@ err_programtext(FILE *fp, int lineno) } fclose(fp); if (i == lineno) { - char *p = linebuf; PyObject *res; - while (*p == ' ' || *p == '\t' || *p == '\014') - p++; - res = PyUnicode_FromString(p); + res = PyUnicode_FromString(linebuf); if (res == NULL) PyErr_Clear(); return res; -- 2.6.2