https://github.com/python/cpython/commit/d0e66ef1c09d4bfece445878a00bcbbd3f9b58e1
commit: d0e66ef1c09d4bfece445878a00bcbbd3f9b58e1
branch: main
author: Hugo van Kemenade <[email protected]>
committer: pablogsal <[email protected]>
date: 2026-03-25T11:08:45Z
summary:
gh-146369: Ensure `PYTHON_LAZY_IMPORTS=none` overrides `__lazy_modules__`
(#146371)
files:
A
Misc/NEWS.d/next/Core_and_Builtins/2026-03-24-13-06-52.gh-issue-146369.6wDI6S.rst
M Lib/test/test_lazy_import/__init__.py
M Python/ceval.c
diff --git a/Lib/test/test_lazy_import/__init__.py
b/Lib/test/test_lazy_import/__init__.py
index a4180f05dbbafc..328f2906f90159 100644
--- a/Lib/test/test_lazy_import/__init__.py
+++ b/Lib/test/test_lazy_import/__init__.py
@@ -1088,6 +1088,49 @@ def
test_env_var_lazy_imports_none_disables_all_lazy(self):
self.assertEqual(result.returncode, 0, f"stderr: {result.stderr}")
self.assertIn("EAGER", result.stdout)
+ def test_cli_lazy_imports_none_disables_dunder_lazy_modules(self):
+ """-X lazy_imports=none should override __lazy_modules__."""
+ code = textwrap.dedent("""
+ import sys
+ __lazy_modules__ = ["json"]
+ import json
+ if 'json' in sys.modules:
+ print("EAGER")
+ else:
+ print("LAZY")
+ """)
+ result = subprocess.run(
+ [sys.executable, "-X", "lazy_imports=none", "-c", code],
+ capture_output=True,
+ text=True,
+ )
+ self.assertEqual(result.returncode, 0, f"stderr: {result.stderr}")
+ self.assertIn("EAGER", result.stdout)
+
+ def test_env_var_lazy_imports_none_disables_dunder_lazy_modules(self):
+ """PYTHON_LAZY_IMPORTS=none should override __lazy_modules__."""
+ code = textwrap.dedent("""
+ import sys
+ __lazy_modules__ = ["json"]
+ import json
+ if 'json' in sys.modules:
+ print("EAGER")
+ else:
+ print("LAZY")
+ """)
+ import os
+
+ env = os.environ.copy()
+ env["PYTHON_LAZY_IMPORTS"] = "none"
+ result = subprocess.run(
+ [sys.executable, "-c", code],
+ capture_output=True,
+ text=True,
+ env=env,
+ )
+ self.assertEqual(result.returncode, 0, f"stderr: {result.stderr}")
+ self.assertIn("EAGER", result.stdout)
+
def test_cli_overrides_env_var(self):
"""Command-line option should take precedence over environment
variable."""
# PEP 810: -X lazy_imports takes precedence over PYTHON_LAZY_IMPORTS
diff --git
a/Misc/NEWS.d/next/Core_and_Builtins/2026-03-24-13-06-52.gh-issue-146369.6wDI6S.rst
b/Misc/NEWS.d/next/Core_and_Builtins/2026-03-24-13-06-52.gh-issue-146369.6wDI6S.rst
new file mode 100644
index 00000000000000..191b7627ed4e56
--- /dev/null
+++
b/Misc/NEWS.d/next/Core_and_Builtins/2026-03-24-13-06-52.gh-issue-146369.6wDI6S.rst
@@ -0,0 +1,2 @@
+Ensure ``-X lazy_imports=none``` and ``PYTHON_LAZY_IMPORTS=none``` override
+``__lazy_modules__``. Patch by Hugo van Kemenade.
diff --git a/Python/ceval.c b/Python/ceval.c
index cb25012ceda92c..2f9195529f2ceb 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -3086,7 +3086,7 @@ _PyEval_LazyImportName(PyThreadState *tstate, PyObject
*builtins,
break;
}
- if (!lazy) {
+ if (!lazy && PyImport_GetLazyImportsMode() != PyImport_LAZY_NONE) {
// See if __lazy_modules__ forces this to be lazy.
lazy = check_lazy_import_compatibility(tstate, globals, name, level);
if (lazy < 0) {
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]