summaryrefslogtreecommitdiffstats
path: root/Python/compile.c
diff options
context:
space:
mode:
authorBatuhan Taskaya <batuhan@python.org>2021-06-30 22:53:36 (GMT)
committerGitHub <noreply@github.com>2021-06-30 22:53:36 (GMT)
commit1b28187a0e3e914ee48de8032cbba0a965dd5563 (patch)
treeebdccc08b86369b90e61eb1066a8af68694589c0 /Python/compile.c
parent66c53b48e1f5c841d9f48e51ce7bf1a74b75b629 (diff)
downloadcpython-1b28187a0e3e914ee48de8032cbba0a965dd5563.zip
cpython-1b28187a0e3e914ee48de8032cbba0a965dd5563.tar.gz
cpython-1b28187a0e3e914ee48de8032cbba0a965dd5563.tar.bz2
bpo-44313: generate LOAD_ATTR/CALL_FUNCTION for top-level imported objects (GH-26677)
Diffstat (limited to 'Python/compile.c')
-rw-r--r--Python/compile.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/Python/compile.c b/Python/compile.c
index 52d2987..99e500f 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -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);