]> granicus.if.org Git - python/commitdiff
Issue #17825: Cursor ^ is correctly positioned for SyntaxError and IndentationError.
authorFlorent Xicluna <florent.xicluna@gmail.com>
Wed, 22 Jan 2014 00:11:43 +0000 (01:11 +0100)
committerFlorent Xicluna <florent.xicluna@gmail.com>
Wed, 22 Jan 2014 00:11:43 +0000 (01:11 +0100)
Lib/test/test_traceback.py
Lib/traceback.py
Misc/NEWS

index 373d9af6418f681aab5c724b30b51501b454b018..9af3b924a18855684cb95b05dd7a4cc40e8b4100 100644 (file)
@@ -35,6 +35,9 @@ class SyntaxTracebackCases(unittest.TestCase):
     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 @@ class SyntaxTracebackCases(unittest.TestCase):
         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 @@ class SyntaxTracebackCases(unittest.TestCase):
         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()
index b13bfe2497cd653511119df3ba410529aabb0233..f33fced7a9d70e12440bd9d1fb30ba385fd732fb 100644 (file)
@@ -227,11 +227,12 @@ def format_exception_only(etype, value):
     if badline is not None:
         lines.append('    %s\n' % 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
-            lines.append('   %s^\n' % ''.join(caretspace))
+            lines.append('    %s^\n' % ''.join(caretspace))
     msg = value.msg or "<no detail available>"
     lines.append("%s: %s\n" % (stype, msg))
     return lines
index 4e3aa45a82e66cb3b65f456759ff0b2e3ab19fa1..66f52207f0f3b425c15ebffdc425a05c8fea4719 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.3.4 release candidate 1?
 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.