[Python-checkins] bpo-43950: support long lines in traceback.py (GH-27336)

isidentical webhook-mailer at python.org
Sat Jul 24 13:50:44 EDT 2021


https://github.com/python/cpython/commit/4f5980a4f57dab68b9137304f58bd08891d43d5a
commit: 4f5980a4f57dab68b9137304f58bd08891d43d5a
branch: main
author: Batuhan Taskaya <batuhan at python.org>
committer: isidentical <isidentical at gmail.com>
date: 2021-07-24T20:50:39+03:00
summary:

bpo-43950: support long lines in traceback.py (GH-27336)

files:
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 c87ce7245335a..5fc5b5926d5a6 100644
--- a/Lib/test/test_traceback.py
+++ b/Lib/test/test_traceback.py
@@ -510,6 +510,28 @@ def test_traceback_specialization_with_syntax_error(self):
         )
         self.assertEqual(result_lines, expected_error.splitlines())
 
+    def test_traceback_very_long_line(self):
+        source = "a" * 256
+        bytecode = compile(source, TESTFN, "exec")
+
+        with open(TESTFN, "w") as file:
+            file.write(source)
+        self.addCleanup(unlink, TESTFN)
+
+        func = partial(exec, bytecode)
+        result_lines = self.get_exception(func)
+
+        lineno_f = bytecode.co_firstlineno
+        expected_error = (
+            'Traceback (most recent call last):\n'
+            f'  File "{__file__}", line {self.callable_line}, in get_exception\n'
+            '    callable()\n'
+            '    ^^^^^^^^^^\n'
+            f'  File "{TESTFN}", line {lineno_f}, in <module>\n'
+            f'    {source}\n'
+        )
+        self.assertEqual(result_lines, expected_error.splitlines())
+
     def assertSpecialized(self, func, expected_specialization):
         result_lines = self.get_exception(func)
         specialization_line = result_lines[-1]
diff --git a/Lib/traceback.py b/Lib/traceback.py
index ae5775d2f3bda..15bdb3c8c2e65 100644
--- a/Lib/traceback.py
+++ b/Lib/traceback.py
@@ -462,7 +462,11 @@ def format_frame(self, frame):
             row.append('    {}\n'.format(frame.line.strip()))
 
             stripped_characters = len(frame._original_line) - len(frame.line.lstrip())
-            if frame.end_lineno == frame.lineno and frame.end_colno != 0:
+            if (
+                frame.end_lineno == frame.lineno
+                and frame.colno is not None
+                and frame.end_colno is not None
+            ):
                 colno = _byte_offset_to_character_offset(frame._original_line, frame.colno)
                 end_colno = _byte_offset_to_character_offset(frame._original_line, frame.end_colno)
 



More information about the Python-checkins mailing list