mirror of
https://github.com/python/cpython.git
synced 2025-09-09 18:32:22 +00:00
gh-98744: Prevent column-level decoding crashes on traceback module (#98824)
This commit is contained in:
parent
7ea10567af
commit
c0f2a5ef91
3 changed files with 63 additions and 14 deletions
|
@ -804,6 +804,56 @@ class TracebackErrorLocationCaretTestBase:
|
||||||
]
|
]
|
||||||
self.assertEqual(actual, expected)
|
self.assertEqual(actual, expected)
|
||||||
|
|
||||||
|
def test_wide_characters_unicode_with_problematic_byte_offset(self):
|
||||||
|
def f():
|
||||||
|
width
|
||||||
|
|
||||||
|
actual = self.get_exception(f)
|
||||||
|
expected = [
|
||||||
|
f"Traceback (most recent call last):",
|
||||||
|
f" File \"{__file__}\", line {self.callable_line}, in get_exception",
|
||||||
|
f" callable()",
|
||||||
|
f" File \"{__file__}\", line {f.__code__.co_firstlineno + 1}, in f",
|
||||||
|
f" width",
|
||||||
|
]
|
||||||
|
self.assertEqual(actual, expected)
|
||||||
|
|
||||||
|
|
||||||
|
def test_byte_offset_with_wide_characters_middle(self):
|
||||||
|
def f():
|
||||||
|
width = 1
|
||||||
|
raise ValueError(width)
|
||||||
|
|
||||||
|
actual = self.get_exception(f)
|
||||||
|
expected = [
|
||||||
|
f"Traceback (most recent call last):",
|
||||||
|
f" File \"{__file__}\", line {self.callable_line}, in get_exception",
|
||||||
|
f" callable()",
|
||||||
|
f" File \"{__file__}\", line {f.__code__.co_firstlineno + 2}, in f",
|
||||||
|
f" raise ValueError(width)",
|
||||||
|
]
|
||||||
|
self.assertEqual(actual, expected)
|
||||||
|
|
||||||
|
def test_byte_offset_multiline(self):
|
||||||
|
def f():
|
||||||
|
www = 1
|
||||||
|
th = 0
|
||||||
|
|
||||||
|
print(1, www(
|
||||||
|
th))
|
||||||
|
|
||||||
|
actual = self.get_exception(f)
|
||||||
|
expected = [
|
||||||
|
f"Traceback (most recent call last):",
|
||||||
|
f" File \"{__file__}\", line {self.callable_line}, in get_exception",
|
||||||
|
f" callable()",
|
||||||
|
f" File \"{__file__}\", line {f.__code__.co_firstlineno + 4}, in f",
|
||||||
|
f" print(1, www(",
|
||||||
|
f" ^^^^",
|
||||||
|
]
|
||||||
|
self.assertEqual(actual, expected)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@requires_debug_ranges()
|
@requires_debug_ranges()
|
||||||
class PurePythonTracebackErrorCaretTests(
|
class PurePythonTracebackErrorCaretTests(
|
||||||
|
|
|
@ -476,32 +476,32 @@ class StackSummary(list):
|
||||||
frame_summary.colno is not None
|
frame_summary.colno is not None
|
||||||
and frame_summary.end_colno is not None
|
and frame_summary.end_colno is not None
|
||||||
):
|
):
|
||||||
colno = _byte_offset_to_character_offset(
|
start_offset = _byte_offset_to_character_offset(
|
||||||
frame_summary._original_line, frame_summary.colno)
|
frame_summary._original_line, frame_summary.colno) + 1
|
||||||
end_colno = _byte_offset_to_character_offset(
|
end_offset = _byte_offset_to_character_offset(
|
||||||
frame_summary._original_line, frame_summary.end_colno)
|
frame_summary._original_line, frame_summary.end_colno) + 1
|
||||||
|
|
||||||
anchors = None
|
anchors = None
|
||||||
if frame_summary.lineno == frame_summary.end_lineno:
|
if frame_summary.lineno == frame_summary.end_lineno:
|
||||||
with suppress(Exception):
|
with suppress(Exception):
|
||||||
anchors = _extract_caret_anchors_from_line_segment(
|
anchors = _extract_caret_anchors_from_line_segment(
|
||||||
frame_summary._original_line[colno - 1:end_colno - 1]
|
frame_summary._original_line[start_offset - 1:end_offset - 1]
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
end_colno = stripped_characters + len(stripped_line)
|
end_offset = stripped_characters + len(stripped_line)
|
||||||
|
|
||||||
# show indicators if primary char doesn't span the frame line
|
# show indicators if primary char doesn't span the frame line
|
||||||
if end_colno - colno < len(stripped_line) or (
|
if end_offset - start_offset < len(stripped_line) or (
|
||||||
anchors and anchors.right_start_offset - anchors.left_end_offset > 0):
|
anchors and anchors.right_start_offset - anchors.left_end_offset > 0):
|
||||||
row.append(' ')
|
row.append(' ')
|
||||||
row.append(' ' * (colno - stripped_characters))
|
row.append(' ' * (start_offset - stripped_characters))
|
||||||
|
|
||||||
if anchors:
|
if anchors:
|
||||||
row.append(anchors.primary_char * (anchors.left_end_offset))
|
row.append(anchors.primary_char * (anchors.left_end_offset))
|
||||||
row.append(anchors.secondary_char * (anchors.right_start_offset - anchors.left_end_offset))
|
row.append(anchors.secondary_char * (anchors.right_start_offset - anchors.left_end_offset))
|
||||||
row.append(anchors.primary_char * (end_colno - colno - anchors.right_start_offset))
|
row.append(anchors.primary_char * (end_offset - start_offset - anchors.right_start_offset))
|
||||||
else:
|
else:
|
||||||
row.append('^' * (end_colno - colno))
|
row.append('^' * (end_offset - start_offset))
|
||||||
|
|
||||||
row.append('\n')
|
row.append('\n')
|
||||||
|
|
||||||
|
@ -561,10 +561,7 @@ class StackSummary(list):
|
||||||
|
|
||||||
def _byte_offset_to_character_offset(str, offset):
|
def _byte_offset_to_character_offset(str, offset):
|
||||||
as_utf8 = str.encode('utf-8')
|
as_utf8 = str.encode('utf-8')
|
||||||
if offset > len(as_utf8):
|
return len(as_utf8[:offset].decode("utf-8", errors="replace"))
|
||||||
offset = len(as_utf8)
|
|
||||||
|
|
||||||
return len(as_utf8[:offset + 1].decode("utf-8"))
|
|
||||||
|
|
||||||
|
|
||||||
_Anchors = collections.namedtuple(
|
_Anchors = collections.namedtuple(
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Prevent crashing in :mod:`traceback` when retrieving the byte-offset for
|
||||||
|
some source files that contain certain unicode characters.
|
Loading…
Add table
Add a link
Reference in a new issue