diff --git a/Lib/test/lazyimports/set_lazy_imports_eager.py b/Lib/test/lazyimports/set_lazy_imports_eager.py index 97db942b3a1..737161d1fe4 100644 --- a/Lib/test/lazyimports/set_lazy_imports_eager.py +++ b/Lib/test/lazyimports/set_lazy_imports_eager.py @@ -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 @@ -29,7 +28,7 @@ self.assertTrue(importlib.is_lazy_import(globals(), "Fred")) # this should be lazy from test.lazyimports.data.metasyntactic.waldo import fred -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 diff --git a/Python/import.c b/Python/import.c index f096fbc826d..46fe96aa0f5 100644 --- a/Python/import.c +++ b/Python/import.c @@ -2886,17 +2886,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)); @@ -2904,7 +2897,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; @@ -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);