From 49c7721f75373b3cb5d89bcf7ff3a424198c19e0 Mon Sep 17 00:00:00 2001 From: johnslavik Date: Tue, 17 Mar 2026 14:20:56 +0100 Subject: [PATCH 1/2] Record simple lazy modules as well --- Lib/test/test_lazy_import/__init__.py | 15 +++++++++++++++ Python/import.c | 6 ++++++ 2 files changed, 21 insertions(+) diff --git a/Lib/test/test_lazy_import/__init__.py b/Lib/test/test_lazy_import/__init__.py index a4180f05dbbafc..043812baad81b6 100644 --- a/Lib/test/test_lazy_import/__init__.py +++ b/Lib/test/test_lazy_import/__init__.py @@ -969,6 +969,21 @@ def test_lazy_modules_is_per_interpreter(self): # Basic test that sys.lazy_modules exists and is a set self.assertIsInstance(sys.lazy_modules, dict) + def test_lazy_module_without_children_is_tracked(self): + code = textwrap.dedent(""" + import sys + lazy import json + assert "json" in sys.lazy_modules, ( + f"expected 'json' in sys.lazy_modules, got {set(sys.lazy_modules)}" + ) + assert sys.lazy_modules["json"] == set(), ( + f"expected empty set for sys.lazy_modules['json'], " + f"got {sys.lazy_modules['json']!r}" + ) + print("OK") + """) + assert_python_ok("-c", code) + @support.requires_subprocess() class CommandLineAndEnvVarTests(unittest.TestCase): diff --git a/Python/import.c b/Python/import.c index 34224f4c6d6514..875fb05afbcf72 100644 --- a/Python/import.c +++ b/Python/import.c @@ -4377,6 +4377,12 @@ register_lazy_on_parent(PyThreadState *tstate, PyObject *name, Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, PyUnicode_GET_LENGTH(name), -1); if (dot < 0) { + PyObject *lazy_submodules = ensure_lazy_submodules( + (PyDictObject *)lazy_modules, name); + if (lazy_submodules == NULL) { + goto done; + } + Py_DECREF(lazy_submodules); ret = 0; goto done; } From 3ee824884e1ce62501fae3ea474e663259ad3feb Mon Sep 17 00:00:00 2001 From: johnslavik Date: Tue, 17 Mar 2026 21:40:59 +0100 Subject: [PATCH 2/2] Add news entry --- .../2026-03-17-14-20-56.gh-issue-145059.aB3xKm.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-03-17-14-20-56.gh-issue-145059.aB3xKm.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-03-17-14-20-56.gh-issue-145059.aB3xKm.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-03-17-14-20-56.gh-issue-145059.aB3xKm.rst new file mode 100644 index 00000000000000..e2db5a83a1a9e3 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-03-17-14-20-56.gh-issue-145059.aB3xKm.rst @@ -0,0 +1 @@ +Fixed ``sys.lazy_modules`` to include lazy modules without submodules. Patch by Bartosz Sławecki.