Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[3.11] gh-99103: Normalize specialized traceback anchors against the current line#99423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
pablogsal merged 1 commit intopython:3.11fromisidentical:backport-57be545-3.11
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletionsLib/test/test_traceback.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -532,6 +532,23 @@ def f_with_binary_operator():
result_lines = self.get_exception(f_with_binary_operator)
self.assertEqual(result_lines, expected_error.splitlines())

def test_caret_for_binary_operators_with_unicode(self):
def f_with_binary_operator():
áóí = 20
return 10 + áóí / 0 + 30

lineno_f = f_with_binary_operator.__code__.co_firstlineno
expected_error = (
'Traceback (most recent call last):\n'
f' File "{__file__}", line {self.callable_line}, in get_exception\n'
' callable()\n'
f' File "{__file__}", line {lineno_f+2}, in f_with_binary_operator\n'
' return 10 + áóí / 0 + 30\n'
' ~~~~^~~\n'
)
result_lines = self.get_exception(f_with_binary_operator)
self.assertEqual(result_lines, expected_error.splitlines())

def test_caret_for_binary_operators_two_char(self):
def f_with_binary_operator():
divisor = 20
Expand DownExpand Up@@ -566,6 +583,23 @@ def f_with_subscript():
result_lines = self.get_exception(f_with_subscript)
self.assertEqual(result_lines, expected_error.splitlines())

def test_caret_for_subscript_unicode(self):
def f_with_subscript():
some_dict = {'ó': {'á': {'í': {'theta': 1}}}}
return some_dict['ó']['á']['í']['beta']

lineno_f = f_with_subscript.__code__.co_firstlineno
expected_error = (
'Traceback (most recent call last):\n'
f' File "{__file__}", line {self.callable_line}, in get_exception\n'
' callable()\n'
f' File "{__file__}", line {lineno_f+2}, in f_with_subscript\n'
" return some_dict['ó']['á']['í']['beta']\n"
' ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^\n'
)
result_lines = self.get_exception(f_with_subscript)
self.assertEqual(result_lines, expected_error.splitlines())

def test_traceback_specialization_with_syntax_error(self):
bytecode = compile("1 / 0 / 1 / 2\n", TESTFN, "exec")

Expand Down
11 changes: 8 additions & 3 deletionsLib/traceback.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -585,12 +585,15 @@ def _extract_caret_anchors_from_line_segment(segment):
if len(tree.body) != 1:
return None

normalize = lambda offset: _byte_offset_to_character_offset(segment, offset)
statement = tree.body[0]
match statement:
case ast.Expr(expr):
match expr:
case ast.BinOp():
operator_str = segment[expr.left.end_col_offset:expr.right.col_offset]
operator_start = normalize(expr.left.end_col_offset)
operator_end = normalize(expr.right.col_offset)
operator_str = segment[operator_start:operator_end]
operator_offset = len(operator_str) - len(operator_str.lstrip())

left_anchor = expr.left.end_col_offset + operator_offset
Expand All@@ -600,9 +603,11 @@ def _extract_caret_anchors_from_line_segment(segment):
and not operator_str[operator_offset + 1].isspace()
):
right_anchor += 1
return _Anchors(left_anchor,right_anchor)
return _Anchors(normalize(left_anchor), normalize(right_anchor))
case ast.Subscript():
return _Anchors(expr.value.end_col_offset, expr.slice.end_col_offset + 1)
subscript_start = normalize(expr.value.end_col_offset)
subscript_end = normalize(expr.slice.end_col_offset + 1)
return _Anchors(subscript_start, subscript_end)

return None

Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
Fix the error reporting positions of specialized traceback anchors when the
source line contains Unicode characters.
9 changes: 7 additions & 2 deletionsPython/traceback.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -705,8 +705,13 @@ extract_anchors_from_line(PyObject *filename, PyObject *line,

done:
if (res > 0) {
*left_anchor += start_offset;
*right_anchor += start_offset;
// Normalize the AST offsets to byte offsets and adjust them with the
// start of the actual line (instead of the source code segment).
assert(segment != NULL);
assert(*left_anchor >= 0);
assert(*right_anchor >= 0);
*left_anchor = _PyPegen_byte_offset_to_character_offset(segment, *left_anchor) + start_offset;
*right_anchor = _PyPegen_byte_offset_to_character_offset(segment, *right_anchor) + start_offset;
}
Py_XDECREF(segment);
if (arena) {
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp