From 5c0d1bcf3fdf9dfa9512a00f8508fb131808409a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Germ=C3=A1n=20M=C3=A9ndez=20Bravo?= Date: Mon, 11 Nov 2024 10:57:37 -0800 Subject: [PATCH] Check the exact full import name for eager imports Summary: We used to check parent import and `fromlist` separately for eagerness. This makes it so we check for the exact full import name on the container instead. This works well with the strategy files introduced in D65434680 and D65555319. Reviewed By: brittanyrey Differential Revision: D65216354 fbshipit-source-id: 3e6972444a370ca981324256904e5d94bc77af4f --- Python/import.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/Python/import.c b/Python/import.c index b078db9a93f..a75be101852 100644 --- a/Python/import.c +++ b/Python/import.c @@ -1637,17 +1637,10 @@ add_lazy_modules(PyThreadState *tstate, PyObject *builtins, PyObject *name, PyOb PyObject *lazy_submodules; if (tstate->interp->eager_imports != NULL) { - int found = PySequence_Contains(tstate->interp->eager_imports, name); - if (found < 0) { - goto error; - } - if (found) { - ret = 0; /* If the module is flagged as eager import, load eagerly */ - goto end; - } + Py_ssize_t size = 0; if (fromlist != NULL && fromlist != Py_None) { assert(PyTuple_CheckExact(fromlist)); - Py_ssize_t size = PyTuple_GET_SIZE(fromlist); + size = PyTuple_GET_SIZE(fromlist); for (Py_ssize_t i = 0; i < size; ++i) { PyObject* item = PyTuple_GET_ITEM(fromlist, i); assert(PyUnicode_Check(item)); @@ -1655,7 +1648,7 @@ add_lazy_modules(PyThreadState *tstate, PyObject *builtins, PyObject *name, PyOb if (from_name == NULL) { goto error; } - found = PySequence_Contains(tstate->interp->eager_imports, from_name); + int found = PySequence_Contains(tstate->interp->eager_imports, from_name); Py_DECREF(from_name); if (found < 0) { goto error; @@ -1666,6 +1659,16 @@ add_lazy_modules(PyThreadState *tstate, PyObject *builtins, PyObject *name, PyOb } } } + if (size == 0) { + int found = PySequence_Contains(tstate->interp->eager_imports, name); + if (found < 0) { + goto error; + } + if (found) { + ret = 0; /* If the module is flagged as eager import, load eagerly */ + goto end; + } + } } lazy_submodules = PyDict_GetItemWithError(lazy_modules, name);