From af5d06e3fc815fbd40d7e3f90143a43de8a68412 Mon Sep 17 00:00:00 2001 From: lazerg Date: Tue, 3 Nov 2026 10:57:36 +0500 Subject: [PATCH 1/2] gh-161229: Accept None for globals and locals in __lazy_import__() --- Lib/test/test_lazy_import/__init__.py | 14 ++++++++++++++ .../2026-11-03-05-52-49.gh-issue-161229.8pOPOI.rst | 2 ++ Python/bltinmodule.c | 4 ++-- 3 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-11-03-05-52-49.gh-issue-161229.8pOPOI.rst diff --git a/Lib/test/test_lazy_import/__init__.py b/Lib/test/test_lazy_import/__init__.py index fd74ce6827fcca..6dd2f7c825e5dd 100644 --- a/Lib/test/test_lazy_import/__init__.py +++ b/Lib/test/test_lazy_import/__init__.py @@ -770,6 +770,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 00000000000000..62ecdfd61fbd72 --- /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 accepts ``None`` for *globals* and *locals*, +matching its signature and :func:`__import__`. diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 5413e57754667f..3018ab1a380cc5 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -312,7 +312,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, @@ -321,7 +321,7 @@ builtin___lazy_import___impl(PyObject *module, PyObject *name, return NULL; } } - if (locals == NULL) { + if (locals == NULL || locals == Py_None) { locals = globals; } From d19ee1b8c9cc66dff6ed9c22ac85d5431c494187 Mon Sep 17 00:00:00 2001 From: lazerg Date: Tue, 3 Nov 2026 10:59:29 +0500 Subject: [PATCH 2/2] Fix NEWS wording --- .../2026-11-03-05-52-49.gh-issue-161229.8pOPOI.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 index 62ecdfd61fbd72..3a9eae1e4af91b 100644 --- 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 @@ -1,2 +1,2 @@ -:func:`!__lazy_import__` now accepts ``None`` for *globals* and *locals*, -matching its signature and :func:`__import__`. +:func:`!__lazy_import__` now treats ``None`` for *globals* and *locals* the +same as omitting them, matching its signature.