mirror of
https://github.com/python/cpython.git
synced 2026-05-06 12:49:07 -04:00
gh-145059: Record lazy modules without submodules in sys.lazy_modules (#146081)
Record simple lazy modules as well
This commit is contained in:
@@ -969,6 +969,21 @@ class SysLazyModulesTrackingTests(unittest.TestCase):
|
||||
# Basic test that sys.lazy_modules exists and is a dict
|
||||
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):
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
Fixed ``sys.lazy_modules`` to include lazy modules without submodules. Patch by Bartosz Sławecki.
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user