[Python-checkins] bpo-45249: Fix caret location when end_offset is set to 0 (GH-28855)
miss-islington
webhook-mailer at python.org
Sat Oct 16 13:51:13 EDT 2021
https://github.com/python/cpython/commit/5df35faf3611b459f7f32bfe9fd2ffa7fb2dc59e
commit: 5df35faf3611b459f7f32bfe9fd2ffa7fb2dc59e
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2021-10-16T10:51:05-07:00
summary:
bpo-45249: Fix caret location when end_offset is set to 0 (GH-28855)
(cherry picked from commit fe0d9e22a52a10c4cbe52254b51f2d4e74d83568)
Co-authored-by: Pablo Galindo Salgado <Pablogsal at gmail.com>
files:
A Misc/NEWS.d/next/Library/2021-10-10-16-14-33.bpo-45249.xqLliz.rst
M Lib/test/test_traceback.py
M Lib/traceback.py
diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py
index fabe5d01a94db..ba03ce46294c5 100644
--- a/Lib/test/test_traceback.py
+++ b/Lib/test/test_traceback.py
@@ -51,6 +51,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)
@@ -81,6 +84,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)
diff --git a/Lib/traceback.py b/Lib/traceback.py
index c7947f118beaa..901b99476aaf8 100644
--- a/Lib/traceback.py
+++ b/Lib/traceback.py
@@ -633,7 +633,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
diff --git a/Misc/NEWS.d/next/Library/2021-10-10-16-14-33.bpo-45249.xqLliz.rst b/Misc/NEWS.d/next/Library/2021-10-10-16-14-33.bpo-45249.xqLliz.rst
new file mode 100644
index 0000000000000..1d5a857e25435
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-10-10-16-14-33.bpo-45249.xqLliz.rst
@@ -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
More information about the Python-checkins
mailing list