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: D65775372

fbshipit-source-id: 322e6fa44cf8adcc7533d6b41b834a244a0f81b1
  • Loading branch information
Kronuz authored and facebook-github-bot committed Nov 13, 2024
1 parent cf9e37a commit 2ee2da1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
13 changes: 6 additions & 7 deletions Lib/test/lazyimports/set_lazy_imports_eager.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,18 @@
import importlib

importlib.set_lazy_imports(eager=[
"test.lazyimports.data.metasyntactic.foo",
"test.lazyimports.data.metasyntactic.waldo",
"test.lazyimports.data.metasyntactic.foo.bar",
"test.lazyimports.data.metasyntactic.plugh.Plugh",
])

import test.lazyimports.data.metasyntactic.foo as foo
self.assertFalse(importlib.is_lazy_import(globals(), "foo")) # should be eager
self.assertTrue(importlib.is_lazy_import(globals(), "foo")) # should be lazy

from test.lazyimports.data.metasyntactic.foo import bar
self.assertFalse(importlib.is_lazy_import(globals(), "bar")) # maybe this should have been lazy?
self.assertFalse(importlib.is_lazy_import(globals(), "bar")) # listed in the eager list, so should not be lazy

from test.lazyimports.data.metasyntactic.waldo import Waldo
self.assertFalse(importlib.is_lazy_import(globals(), "Waldo")) # maybe this should have been lazy?
from test.lazyimports.data.metasyntactic.foo.bar import Bar
self.assertTrue(importlib.is_lazy_import(globals(), "Bar")) # should be lazy

import test.lazyimports.data.metasyntactic.waldo.fred as fred
self.assertTrue(importlib.is_lazy_import(globals(), "fred")) # this should be lazy
Expand All @@ -29,7 +28,7 @@
self.assertTrue(importlib.is_lazy_import(globals(), "Fred")) # this should be lazy

from test.lazyimports.data.metasyntactic.waldo import fred

Check failure on line 30 in Lib/test/lazyimports/set_lazy_imports_eager.py

View workflow job for this annotation

GitHub Actions / lint

Ruff (F811)

Lib/test/lazyimports/set_lazy_imports_eager.py:30:55: F811 Redefinition of unused `fred` from line 24
self.assertFalse(importlib.is_lazy_import(globals(), "fred")) # maybe this should have been lazy?
self.assertTrue(importlib.is_lazy_import(globals(), "fred")) # this should be lazy

import test.lazyimports.data.metasyntactic.plugh as plugh
self.assertTrue(importlib.is_lazy_import(globals(), "plugh")) # this should be lazy
Expand Down
23 changes: 13 additions & 10 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -2886,25 +2886,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 @@ -2915,6 +2908,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 2ee2da1

Please sign in to comment.