[Python-checkins] cpython (merge 3.5 -> 3.6): Issue #25677: Merge SyntaxError caret positioning from 3.5

martin.panter python-checkins at python.org
Mon Dec 19 01:47:04 EST 2016


https://hg.python.org/cpython/rev/d4effdd699de
changeset: 105734:d4effdd699de
branch: 3.6
parent: 105731:7bc2923a41b6
parent: 105733:35b50c26f780
user: Martin Panter <vadmium+py at gmail.com>
date: Mon Dec 19 06:46:01 2016 +0000
summary:
 Issue #25677: Merge SyntaxError caret positioning from 3.5
files:
 Lib/test/test_cmd_line_script.py | 33 ++++++++++++++++++++
 Misc/ACKS | 1 +
 Misc/NEWS | 3 +
 Python/errors.c | 5 +--
 Python/pythonrun.c | 2 +-
 5 files changed, 39 insertions(+), 5 deletions(-)
diff --git a/Lib/test/test_cmd_line_script.py b/Lib/test/test_cmd_line_script.py
--- a/Lib/test/test_cmd_line_script.py
+++ b/Lib/test/test_cmd_line_script.py
@@ -10,6 +10,7 @@
 import os.path
 import py_compile
 import subprocess
+import io
 
 import textwrap
 from test import support
@@ -539,6 +540,38 @@
 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)
+
+ # 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():
 support.run_unittest(CmdLineTest)
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -851,6 +851,7 @@
 Chris Lawrence
 Mark Lawrence
 Chris Laws
+Michael Layzell
 Michael Lazar
 Brian Leair
 Mathieu Leduc-Hamel
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
 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
 form.
 
diff --git a/Python/errors.c b/Python/errors.c
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -1144,11 +1144,8 @@
 }
 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;
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -528,7 +528,7 @@
 offset -= (int)(nl+1-text);
 text = nl+1;
 }
- while (*text == ' ' || *text == '\t') {
+ while (*text == ' ' || *text == '\t' || *text == '\f') {
 text++;
 offset--;
 }
-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /