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

florent.xicluna python-checkins at python.org
Wed Jan 22 01:34:14 CET 2014


http://hg.python.org/cpython/rev/0041be34edbf
changeset:   88626:0041be34edbf
branch:      2.7
parent:      88622:b6c5a37b221f
user:        Florent Xicluna <florent.xicluna at gmail.com>
date:        Wed Jan 22 01:33:59 2014 +0100
summary:
  Issue #17825: Cursor ^ is correctly positioned for SyntaxError and IndentationError.

files:
  Lib/test/test_traceback.py |  10 ++++++++++
  Lib/traceback.py           |   7 ++++---
  Misc/NEWS                  |   3 +++
  3 files changed, 17 insertions(+), 3 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_bad_indentation(self):
         compile("def spam():\n  print 1\n print 2", "?", "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)
@@ -111,6 +114,13 @@
                 os.unlink(os.path.join(testdir, f))
             os.rmdir(testdir)
 
+        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
@@ -189,11 +189,12 @@
         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))
         value = msg
 
     lines.append(_format_final_exc_line(stype, value))
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -9,6 +9,9 @@
 Core and Builtins
 -----------------
 
+- Issue #17825: Cursor "^" is correctly positioned for SyntaxError and
+  IndentationError.
+
 - Issue #19081: When a zipimport .zip file in sys.path being imported from
   is modified during the lifetime of the Python process after zipimport has
   already cached the zip's table of contents we detect this and recover

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list