8000 bpo-43950: support long lines in traceback.py (GH-27336) · python/cpython@4f5980a · GitHub
[go: up one dir, main page]

Skip to content

Commit 4f5980a

Browse files
authored
bpo-43950: support long lines in traceback.py (GH-27336)
1 parent ef8b853 commit 4f5980a

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

Lib/test/test_traceback.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,28 @@ def test_traceback_specialization_with_syntax_error(self):
510510
)
511511
self.assertEqual(result_lines, expected_error.splitlines())
512512

513+
def test_traceback_very_long_line(self):
514+
source = "a" * 256
515+
bytecode = compile(source, TESTFN, "exec")
516+
517+
with open(TESTFN, "w") as file:
518+
file.write(source)
519+
self.addCleanup(unlink, TESTFN)
520+
521+
func = partial(exec, bytecode)
522+
result_lines = self.get_exception(func)
523+
524+
lineno_f = bytecode.co_firstlineno
525+
expected_error = (
526+
'Traceback (most recent call last):\n'
527+
f' File "{__file__}", line {self.callable_line}, in get_exception\n'
528+
' callable()\n'
529+
' ^^^^^^^^^^\n'
530+
f' File "{TESTFN}", line {lineno_f}, in <module>\n'
531+
f' {source}\n'
532+
)
533+
self.assertEqual(result_lines, expected_error.splitlines())
534+
513535
def assertSpecialized(self, func, expected_specialization):
514536
result_lines = self.get_exception(func)
515537
specialization_line = result_lines[-1]

Lib/traceback.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,11 @@ def format_frame(self, frame):
462462
row.append(' {}\n'.format(frame.line.strip()))
463463

464464
stripped_characters = len(frame._original_line) - len(frame.line.lstrip())
465-
if frame.end_lineno == frame.lineno and frame.end_colno != 0:
465+
if (
466+
frame.end_lineno == frame.lineno
467+
and frame.colno is not None
468+
and frame.end_colno is not None
469+
):
466470
colno = _byte_offset_to_character_offset(frame._original_line, frame.colno)
467471
end_colno = _byte_offset_to_character_offset(frame._original_line, frame.end_colno)
468472

0 commit comments

Comments
 (0)
0