FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-45249: Fix caret location when end_offset is set to 0 by pablogsal · Pull Request #28854 · python/cpython · GitHub

/ cpython Public
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
10 changes: 10 additions & 0 deletions Lib/test/test_traceback.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ def syntax_error_with_caret_non_ascii(self):
def syntax_error_bad_indentation2(self):
compile(" print(2)", "?", "exec")

def tokenizer_error_with_caret_range(self):
compile("blech ( ", "?", "exec")

def test_caret(self):
err = self.get_exception_format(self.syntax_error_with_caret,
SyntaxError)
Expand Down Expand Up @@ -85,6 +88,13 @@ def test_caret(self):
self.assertEqual(err[1].find("y"), err[2].find("^")) # in the right place
self.assertEqual(err[2].count("^"), len("y for y in range(30)"))

err = self.get_exception_format(self.tokenizer_error_with_caret_range,
SyntaxError)
self.assertIn("^", err[2]) # third line has caret
self.assertEqual(err[2].count('\n'), 1) # and no additional newline
self.assertEqual(err[1].find("("), err[2].find("^")) # in the right place
self.assertEqual(err[2].count("^"), 1)

def test_nocaret(self):
exc = SyntaxError("error", ("x.py", 23, None, "bad syntax"))
err = traceback.format_exception_only(SyntaxError, exc)
Expand Down
2 changes: 1 addition & 1 deletion Lib/traceback.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ def _format_syntax_error(self, stype):

if self.offset is not None:
offset = self.offset
end_offset = self.end_offset if self.end_offset is not None else offset
end_offset = self.end_offset if self.end_offset not in {None, 0} else offset
if offset == end_offset or end_offset == -1:
end_offset = offset + 1

Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix the behaviour of :func:`traceback.print_exc` when displaying the caret
when the ``end_offset`` in the exception is set to 0. Patch by Pablo Galindo

Back | FazBrowse Home | New Git URL