bpo-44313: generate LOAD_ATTR/CALL_FUNCTION for top-level imported objects (GH-26677)

This commit is contained in:
Batuhan Taskaya 2021-07-01 01:53:36 +03:00 committed by GitHub
parent 66c53b48e1
commit 1b28187a0e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 279 additions and 214 deletions

View file

@ -4278,6 +4278,23 @@ check_index(struct compiler *c, expr_ty e, expr_ty s)
}
}
static int
is_import_originated(struct compiler *c, expr_ty e)
{
/* Check whether the global scope has an import named
e, if it is a Name object. For not traversing all the
scope stack every time this function is called, it will
only check the global scope to determine whether something
is imported or not. */
if (e->kind != Name_kind) {
return 0;
}
long flags = _PyST_GetSymbol(c->c_st->st_top, e->v.Name.id);
return flags & DEF_IMPORT;
}
// Return 1 if the method call was optimized, -1 if not, and 0 on error.
static int
maybe_optimize_method_call(struct compiler *c, expr_ty e)
@ -4291,6 +4308,12 @@ maybe_optimize_method_call(struct compiler *c, expr_ty e)
if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load) {
return -1;
}
/* Check that the base object is not something that is imported */
if (is_import_originated(c, meth->v.Attribute.value)) {
return -1;
}
/* Check that there aren't too many arguments */
argsl = asdl_seq_LEN(args);
kwdsl = asdl_seq_LEN(kwds);