]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-146369: Ensure `PYTHON_LAZY_IMPORTS=none` overrides `__lazy_modules__` (#146371)
authorHugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Wed, 25 Mar 2026 11:08:45 +0000 (13:08 +0200)
committerGitHub <noreply@github.com>
Wed, 25 Mar 2026 11:08:45 +0000 (11:08 +0000)
Lib/test/test_lazy_import/__init__.py
Misc/NEWS.d/next/Core_and_Builtins/2026-03-24-13-06-52.gh-issue-146369.6wDI6S.rst [new file with mode: 0644]
Python/ceval.c

index a4180f05dbbafc4ac72a8dfe49f35cd1e91a1441..328f2906f901597d155f99ad66a6b553aa849853 100644 (file)
@@ -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 (file)
index 0000000..191b762
--- /dev/null
@@ -0,0 +1,2 @@
+Ensure ``-X lazy_imports=none``` and ``PYTHON_LAZY_IMPORTS=none``` override
+``__lazy_modules__``. Patch by Hugo van Kemenade.
index cb25012ceda92cf564237ad7165cc847362831df..2f9195529f2ceb6f9fabe822b25ab6233dde3d3f 100644 (file)
@@ -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) {