10000 bpo-39316: Make sure that attribute accesses and stores, including method calls, conform to PEP 626. by markshannon · Pull Request #24859 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-39316: Make sure that attribute accesses and stores, including method calls, conform to PEP 626. #24859

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 4 commits into from
Mar 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Make sure that attribute accesses and stores, including method calls,…
… conform to PEP 626.
  • Loading branch information
markshannon committed Mar 14, 2021
commit b844e79deed718caef41b225e1057ff5d32ec17d
46 changes: 46 additions & 0 deletions Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,52 @@ def no_code2():
self.assertEqual(end, len(code.co_code))
self.assertEqual(line, code.co_firstlineno)

def test_lineno_attribute(self):
def load_attr():
return (
o.
a
)
load_attr_lines = [ 2, 3, 1 ]

def load_method():
return (
o.
m(
0
)
)
load_method_lines = [ 2, 3, 4, 3, 1 ]


def store_attr():
(
o.
a
) = (
v
)
store_attr_lines = [ 5, 2, 3 ]

def aug_store_attr():
(
o.
a
) += (
v
)
aug_store_attr_lines = [ 2, 3, 5, 1, 3 ]

funcs = [ load_attr, load_method, store_attr, aug_store_attr]
func_lines = [ load_attr_lines, load_method_lines,
store_attr_lines, aug_store_attr_lines]

for func, lines in zip(funcs, func_lines, strict=True):
with self.subTest(func=func):
code_lines = [ line-func.__code__.co_firstlineno
for (_, _, line) in func.__code__.co_lines() ]
self.assertEqual(lines, code_lines)


def test_big_dict_literal(self):
# The compiler has a flushing point in "compiler_dict" that calls compiles
Expand Down
16 changes: 16 additions & 0 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -4145,9 +4145,12 @@ maybe_optimize_method_call(struct compiler *c, expr_ty e)

/* Alright, we can optimize the code. */
VISIT(c, expr, meth->v.Attribute.value);
int old_lineno = c->u->u_lineno;
c->u->u_lineno = meth->end_lineno;
ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names);
VISIT_SEQ(c, expr, e->v.Call.args);
ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args));
c->u->u_lineno = old_lineno;
return 1;
}

Expand Down Expand Up @@ -5113,12 +5116,21 @@ compiler_visit_expr1(struct compiler *c, expr_ty e)
VISIT(c, expr, e->v.Attribute.value);
switch (e->v.Attribute.ctx) {
case Load:
{
int old_lineno = c->u->u_lineno;
c->u->u_lineno = e->end_lineno;
ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
c->u->u_lineno = old_lineno;
break;
}
case Store:
if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx))
return 0;

int old_lineno = c->u->u_lineno;
c->u->u_lineno = e->end_lineno;
ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
c->u->u_lineno = old_lineno;
break;
case Del:
ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
Expand Down Expand Up @@ -5182,7 +5194,10 @@ compiler_augassign(struct compiler *c, stmt_ty s)
case Attribute_kind:
VISIT(c, expr, e->v.Attribute.value);
ADDOP(c, DUP_TOP);
int old_lineno = c->u->u_lineno;
c->u->u_lineno = e->end_lineno;
ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
c->u->u_lineno = old_lineno;
break;
case Subscript_kind:
VISIT(c, expr, e->v.Subscript.value);
Expand Down Expand Up @@ -5211,6 +5226,7 @@ compiler_augassign(struct compiler *c, stmt_ty s)

switch (e->kind) {
case Attribute_kind:
c->u->u_lineno = e->end_lineno;
ADDOP(c, ROT_TWO);
ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
break;
Expand Down
0