From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Wed, 25 Mar 2026 11:08:45 +0000 (+0200) Subject: gh-146369: Ensure `PYTHON_LAZY_IMPORTS=none` overrides `__lazy_modules__` (#146371) X-Git-Tag: v3.15.0a8~181 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d0e66ef1c09d4bfece445878a00bcbbd3f9b58e1;p=thirdparty%2FPython%2Fcpython.git gh-146369: Ensure `PYTHON_LAZY_IMPORTS=none` overrides `__lazy_modules__` (#146371) --- diff --git a/Lib/test/test_lazy_import/__init__.py b/Lib/test/test_lazy_import/__init__.py index a4180f05dbba..328f2906f901 100644 --- a/Lib/test/test_lazy_import/__init__.py +++ b/Lib/test/test_lazy_import/__init__.py @@ -1088,6 +1088,49 @@ class CommandLineAndEnvVarTests(unittest.TestCase): 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 000000000000..191b7627ed4e --- /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 cb25012ceda9..2f9195529f2c 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) {