Skip to content

Commit 49c7721

Browse files
committed
Record simple lazy modules as well
1 parent 99e2c5e commit 49c7721

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

Lib/test/test_lazy_import/__init__.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,21 @@ def test_lazy_modules_is_per_interpreter(self):
969969
# Basic test that sys.lazy_modules exists and is a set
970970
self.assertIsInstance(sys.lazy_modules, dict)
971971

972+
def test_lazy_module_without_children_is_tracked(self):
973+
code = textwrap.dedent("""
974+
import sys
975+
lazy import json
976+
assert "json" in sys.lazy_modules, (
977+
f"expected 'json' in sys.lazy_modules, got {set(sys.lazy_modules)}"
978+
)
979+
assert sys.lazy_modules["json"] == set(), (
980+
f"expected empty set for sys.lazy_modules['json'], "
981+
f"got {sys.lazy_modules['json']!r}"
982+
)
983+
print("OK")
984+
""")
985+
assert_python_ok("-c", code)
986+
972987

973988
@support.requires_subprocess()
974989
class CommandLineAndEnvVarTests(unittest.TestCase):

Python/import.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4377,6 +4377,12 @@ register_lazy_on_parent(PyThreadState *tstate, PyObject *name,
43774377
Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
43784378
PyUnicode_GET_LENGTH(name), -1);
43794379
if (dot < 0) {
4380+
PyObject *lazy_submodules = ensure_lazy_submodules(
4381+
(PyDictObject *)lazy_modules, name);
4382+
if (lazy_submodules == NULL) {
4383+
goto done;
4384+
}
4385+
Py_DECREF(lazy_submodules);
43804386
ret = 0;
43814387
goto done;
43824388
}

0 commit comments

Comments
 (0)