Skip to content

Commit

Permalink
Check the exact full import name for eager imports
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Kronuz authored and facebook-github-bot committed Nov 11, 2024
1 parent 6d0e474 commit 5c0d1bc
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -1637,25 +1637,18 @@ 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));
PyObject *from_name = PyUnicode_FromFormat("%U.%U", name, item);
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;
Expand All @@ -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);
Expand Down

0 comments on commit 5c0d1bc

Please sign in to comment.