https://github.com/python/cpython/commit/ae86660de336be36c7761bde8af1d5f54499ad9b
commit: ae86660de336be36c7761bde8af1d5f54499ad9b
branch: 3.15
author: Miss Islington (bot) <[email protected]>
committer: sobolevn <[email protected]>
date: 2026-11-04T14:28:37Z
summary:

[3.15] gh-161229: Accept `None` for `globals` and `locals` in 
`__lazy_import__()` (GH-161231) (#161315)

gh-161229: Accept `None` for `globals` and `locals` in `__lazy_import__()` 
(GH-161231)
(cherry picked from commit 1941319efb6fa4184b7fa14a8638a870530ffe20)

Co-authored-by: Lazizbek Ergashev <[email protected]>

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2026-11-03-05-52-49.gh-issue-161229.8pOPOI.rst
M Lib/test/test_lazy_import/__init__.py
M Python/bltinmodule.c

diff --git a/Lib/test/test_lazy_import/__init__.py 
b/Lib/test/test_lazy_import/__init__.py
index 59ff1dee53ba508..b6ea22e62545df6 100644
--- a/Lib/test/test_lazy_import/__init__.py
+++ b/Lib/test/test_lazy_import/__init__.py
@@ -778,6 +778,20 @@ def test_dunder_lazy_import_invalid_arguments(self):
             b"TypeError: Item in ``from list'' must be str, not int",
             result.err)
 
+    @support.requires_subprocess()
+    def test_dunder_lazy_import_explicit_none(self):
+        """__lazy_import__ should accept None for globals and locals."""
+        code = textwrap.dedent("""
+            import sys
+            import types
+
+            lazy = __lazy_import__("test.test_lazy_import.data.basic2",
+                                   globals=None, locals=None)
+            assert type(globals()["lazy"]) is types.LazyImportType
+            assert "test.test_lazy_import.data.basic2" not in sys.modules
+        """)
+        assert_python_ok("-c", code)
+
     def test_dunder_lazy_import_builtins(self):
         """__lazy_import__ should use module's __builtins__ for __import__."""
         from test.test_lazy_import.data import dunder_lazy_import_builtins
diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-03-05-52-49.gh-issue-161229.8pOPOI.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-03-05-52-49.gh-issue-161229.8pOPOI.rst
new file mode 100644
index 000000000000000..3a9eae1e4af91ba
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-03-05-52-49.gh-issue-161229.8pOPOI.rst
@@ -0,0 +1,2 @@
+:func:`!__lazy_import__` now treats ``None`` for *globals* and *locals* the
+same as omitting them, matching its signature.
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index a90cd60f636f4e3..4a1f97d90cac09d 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -311,7 +311,7 @@ builtin___lazy_import___impl(PyObject *module, PyObject 
*name,
 {
     PyObject *builtins;
     PyThreadState *tstate = PyThreadState_GET();
-    if (globals == NULL) {
+    if (globals == NULL || globals == Py_None) {
         globals = PyEval_GetGlobals();
         if (globals == NULL) {
             PyErr_SetString(PyExc_TypeError,
@@ -320,7 +320,7 @@ builtin___lazy_import___impl(PyObject *module, PyObject 
*name,
             return NULL;
         }
     }
-    if (locals == NULL) {
+    if (locals == NULL || locals == Py_None) {
         locals = globals;
     }
 

_______________________________________________
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]

Reply via email to