Skip to content

GH-94694: Fix column offsets for multi-line method lookups #94697

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 10, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
21 changes: 21 additions & 0 deletions Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,27 @@ def test_complex_single_line_expression(self):
self.assertOpcodeSourcePositionIs(compiled_code, 'BINARY_OP',
line=1, end_line=1, column=0, end_column=27, occurrence=4)

def test_multiline_assert_rewritten_as_method_call(self):
# GH-94694: Copying location information from a "real" node to a
# handwritten one should always be valid!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it really always be valid?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can remove the comment if it's too controversial. It just seems to me that the utility/safety of ast.copy_location and ast.fix_missing_locations is dramatically reduced if they can't at least promise crash-free behavior.

tree = ast.parse("assert (\n42\n)")
old_node = tree.body[0]
new_node = ast.Expr(
ast.Call(
ast.Attribute(
ast.Name("spam", ast.Load()),
"eggs",
ast.Load(),
),
[],
[],
)
)
ast.copy_location(new_node, old_node)
ast.fix_missing_locations(new_node)
tree.body[0] = new_node
compile(tree, "<test>", "exec")


class TestExpressionStackSize(unittest.TestCase):
# These tests check that the computed stack size for a code object
Expand Down
51 changes: 51 additions & 0 deletions Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,57 @@ class A: pass
)
self.assertEqual(result_lines, expected_error.splitlines())

def test_multiline_method_call_a(self):
def f():
(None
.method
)()
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" ^^^^^^^^^^",
f" File \"{__file__}\", line {f.__code__.co_firstlineno + 2}, in f",
f" .method",
f" ^^^^^^",
]
self.assertEqual(actual, expected)

def test_multiline_method_call_b(self):
def f():
(None.
method
)()
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" ^^^^^^^^^^",
f" File \"{__file__}\", line {f.__code__.co_firstlineno + 2}, in f",
f" method",
f" ^^^^^^",
]
self.assertEqual(actual, expected)

def test_multiline_method_call_c(self):
def f():
(None
. method
)()
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" ^^^^^^^^^^",
f" File \"{__file__}\", line {f.__code__.co_firstlineno + 2}, in f",
f" . method",
f" ^^^^^^",
]
self.assertEqual(actual, expected)

@cpython_only
@requires_debug_ranges()
class CPythonTracebackErrorCaretTests(TracebackErrorLocationCaretTests):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix an issue that could cause code with multi-line method lookups to have
misleading or incorrect column offset information. In some cases (when
compiling a hand-built AST) this could have resulted in a hard crash of the
interpreter.
14 changes: 13 additions & 1 deletion Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -4737,7 +4737,19 @@ update_location_to_match_attr(struct compiler *c, expr_ty meth)
if (meth->lineno != meth->end_lineno) {
// Make start location match attribute
c->u->u_loc.lineno = meth->end_lineno;
c->u->u_loc.col_offset = meth->end_col_offset - (int)PyUnicode_GetLength(meth->v.Attribute.attr)-1;
int len = (int)PyUnicode_GET_LENGTH(meth->v.Attribute.attr);
// We have no idea where the dot is. Don't try to include it in the
// column span, it's more trouble than it's worth:
if (len <= meth->end_col_offset) {
// |---- end_col_offset
// .method(...)
// |---------- new col_offset
c->u->u_loc.col_offset = meth->end_col_offset - len;
}
else {
// GH-94694: Somebody's compiling weird ASTs. Just drop the columns:
c->u->u_loc.col_offset = c->u->u_loc.end_col_offset = -1;
}
}
}

Expand Down