[Python-checkins] cpython (merge 3.3 -> default): Issue #17825: Cursor ^ is correctly positioned for SyntaxError and

florent.xicluna python-checkins at python.org
Wed Jan 22 01:17:10 CET 2014


http://hg.python.org/cpython/rev/aeb204b8f6c4
changeset: 88625:aeb204b8f6c4
parent: 88623:ca99518092a5
parent: 88624:6d1372237607
user: Florent Xicluna <florent.xicluna at gmail.com>
date: Wed Jan 22 01:16:25 2014 +0100
summary:
 Issue #17825: Cursor ^ is correctly positioned for SyntaxError and IndentationError.
files:
 Lib/test/test_traceback.py | 18 ++++++++++++++----
 Lib/traceback.py | 7 ++++---
 Misc/NEWS | 3 +++
 3 files changed, 21 insertions(+), 7 deletions(-)
diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py
--- a/Lib/test/test_traceback.py
+++ b/Lib/test/test_traceback.py
@@ -35,6 +35,9 @@
 def syntax_error_with_caret_non_ascii(self):
 compile('Python = "\u1e54\xfd\u0163\u0125\xf2\xf1" +', "?", "exec")
 
+ def syntax_error_bad_indentation2(self):
+ compile(" print(2)", "?", "exec")
+
 def test_caret(self):
 err = self.get_exception_format(self.syntax_error_with_caret,
 SyntaxError)
@@ -46,14 +49,14 @@
 err = self.get_exception_format(self.syntax_error_with_caret_2,
 SyntaxError)
 self.assertIn("^", err[2]) # third line has caret
- self.assertTrue(err[2].count('\n') == 1) # and no additional newline
- self.assertTrue(err[1].find("+") == err[2].find("^")) # in the right place
+ self.assertEqual(err[2].count('\n'), 1) # and no additional newline
+ self.assertEqual(err[1].find("+"), err[2].find("^")) # in the right place
 
 err = self.get_exception_format(self.syntax_error_with_caret_non_ascii,
 SyntaxError)
 self.assertIn("^", err[2]) # third line has caret
- self.assertTrue(err[2].count('\n') == 1) # and no additional newline
- self.assertTrue(err[1].find("+") == err[2].find("^")) # in the right place
+ self.assertEqual(err[2].count('\n'), 1) # and no additional newline
+ self.assertEqual(err[1].find("+"), err[2].find("^")) # in the right place
 
 def test_nocaret(self):
 exc = SyntaxError("error", ("x.py", 23, None, "bad syntax"))
@@ -69,6 +72,13 @@
 self.assertIn("^", err[2])
 self.assertEqual(err[1].find(")"), err[2].find("^"))
 
+ err = self.get_exception_format(self.syntax_error_bad_indentation2,
+ IndentationError)
+ self.assertEqual(len(err), 4)
+ self.assertEqual(err[1].strip(), "print(2)")
+ self.assertIn("^", err[2])
+ self.assertEqual(err[1].find("p"), err[2].find("^"))
+
 def test_base_exception(self):
 # Test that exceptions derived from BaseException are formatted right
 e = KeyboardInterrupt()
diff --git a/Lib/traceback.py b/Lib/traceback.py
--- a/Lib/traceback.py
+++ b/Lib/traceback.py
@@ -224,11 +224,12 @@
 if badline is not None:
 yield ' {}\n'.format(badline.strip())
 if offset is not None:
- caretspace = badline.rstrip('\n')[:offset].lstrip()
+ caretspace = badline.rstrip('\n')
+ offset = min(len(caretspace), offset) - 1
+ caretspace = caretspace[:offset].lstrip()
 # non-space whitespace (likes tabs) must be kept for alignment
 caretspace = ((c.isspace() and c or ' ') for c in caretspace)
- # only three spaces to account for offset1 == pos 0
- yield ' {}^\n'.format(''.join(caretspace))
+ yield ' {}^\n'.format(''.join(caretspace))
 msg = value.msg or "<no detail available>"
 yield "{}: {}\n".format(stype, msg)
 
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
 Core and Builtins
 -----------------
 
+- Issue #17825: Cursor "^" is correctly positioned for SyntaxError and
+ IndentationError.
+
 - Issue #2382: SyntaxError cursor "^" is now written at correct position in most
 cases when multibyte characters are in line (before "^"). This still not
 works correctly with wide East Asian characters.
-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list

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