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

Appearance settings

Commit 264b3dd

Browse filesBrowse files
authored
GH-94694: Fix column offsets for multi-line method lookups (GH-94697)
1 parent a10cf2f commit 264b3dd
Copy full SHA for 264b3dd

File tree

Expand file treeCollapse file tree

4 files changed

+85
-2
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+85
-2
lines changed

‎Lib/test/test_compile.py

Copy file name to clipboardExpand all lines: Lib/test/test_compile.py
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,27 @@ def test_complex_single_line_expression(self):
11811181
self.assertOpcodeSourcePositionIs(compiled_code, 'BINARY_OP',
11821182
line=1, end_line=1, column=0, end_column=27, occurrence=4)
11831183

1184+
def test_multiline_assert_rewritten_as_method_call(self):
1185+
# GH-94694: Don't crash if pytest rewrites a multiline assert as a
1186+
# method call with the same location information:
1187+
tree = ast.parse("assert (\n42\n)")
1188+
old_node = tree.body[0]
1189+
new_node = ast.Expr(
1190+
ast.Call(
1191+
ast.Attribute(
1192+
ast.Name("spam", ast.Load()),
1193+
"eggs",
1194+
ast.Load(),
1195+
),
1196+
[],
1197+
[],
1198+
)
1199+
)
1200+
ast.copy_location(new_node, old_node)
1201+
ast.fix_missing_locations(new_node)
1202+
tree.body[0] = new_node
1203+
compile(tree, "<test>", "exec")
1204+
11841205

11851206
class TestExpressionStackSize(unittest.TestCase):
11861207
# These tests check that the computed stack size for a code object

‎Lib/test/test_traceback.py

Copy file name to clipboardExpand all lines: Lib/test/test_traceback.py
+51Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,57 @@ class A: pass
702702
)
703703
self.assertEqual(result_lines, expected_error.splitlines())
704704

705+
def test_multiline_method_call_a(self):
706+
def f():
707+
(None
708+
.method
709+
)()
710+
actual = self.get_exception(f)
711+
expected = [
712+
f"Traceback (most recent call last):",
713+
f" File \"{__file__}\", line {self.callable_line}, in get_exception",
714+
f" callable()",
715+
f" ^^^^^^^^^^",
716+
f" File \"{__file__}\", line {f.__code__.co_firstlineno + 2}, in f",
717+
f" .method",
718+
f" ^^^^^^",
719+
]
720+
self.assertEqual(actual, expected)
721+
722+
def test_multiline_method_call_b(self):
723+
def f():
724+
(None.
725+
method
726+
)()
727+
actual = self.get_exception(f)
728+
expected = [
729+
f"Traceback (most recent call last):",
730+
f" File \"{__file__}\", line {self.callable_line}, in get_exception",
731+
f" callable()",
732+
f" ^^^^^^^^^^",
733+
f" File \"{__file__}\", line {f.__code__.co_firstlineno + 2}, in f",
734+
f" method",
735+
f" ^^^^^^",
736+
]
737+
self.assertEqual(actual, expected)
738+
739+
def test_multiline_method_call_c(self):
740+
def f():
741+
(None
742+
. method
743+
)()
744+
actual = self.get_exception(f)
745+
expected = [
746+
f"Traceback (most recent call last):",
747+
f" File \"{__file__}\", line {self.callable_line}, in get_exception",
748+
f" callable()",
749+
f" ^^^^^^^^^^",
750+
f" File \"{__file__}\", line {f.__code__.co_firstlineno + 2}, in f",
751+
f" . method",
752+
f" ^^^^^^",
753+
]
754+
self.assertEqual(actual, expected)
755+
705756
@cpython_only
706757
@requires_debug_ranges()
707758
class CPythonTracebackErrorCaretTests(TracebackErrorLocationCaretTests):
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix an issue that could cause code with multi-line method lookups to have
2+
misleading or incorrect column offset information. In some cases (when
3+
compiling a hand-built AST) this could have resulted in a hard crash of the
4+
interpreter.

‎Python/compile.c

Copy file name to clipboardExpand all lines: Python/compile.c
+9-2Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4736,8 +4736,15 @@ update_location_to_match_attr(struct compiler *c, expr_ty meth)
47364736
{
47374737
if (meth->lineno != meth->end_lineno) {
47384738
// Make start location match attribute
4739-
c->u->u_loc.lineno = meth->end_lineno;
4740-
c->u->u_loc.col_offset = meth->end_col_offset - (int)PyUnicode_GetLength(meth->v.Attribute.attr)-1;
4739+
c->u->u_loc.lineno = c->u->u_loc.end_lineno = meth->end_lineno;
4740+
int len = (int)PyUnicode_GET_LENGTH(meth->v.Attribute.attr);
4741+
if (len <= meth->end_col_offset) {
4742+
c->u->u_loc.col_offset = meth->end_col_offset - len;
4743+
}
4744+
else {
4745+
// GH-94694: Somebody's compiling weird ASTs. Just drop the columns:
4746+
c->u->u_loc.col_offset = c->u->u_loc.end_col_offset = -1;
4747+
}
47414748
}
47424749
}
47434750

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.