Skip to content

Commit af5d06e

Browse files
committed
gh-161229: Accept None for globals and locals in __lazy_import__()
1 parent 3945eaf commit af5d06e

3 files changed

Lines changed: 18 additions & 2 deletions

File tree

‎Lib/test/test_lazy_import/__init__.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,20 @@ def test_dunder_lazy_import_invalid_arguments(self):
770770
b"TypeError: Item in ``from list'' must be str, not int",
771771
result.err)
772772

773+
@support.requires_subprocess()
774+
def test_dunder_lazy_import_explicit_none(self):
775+
"""__lazy_import__ should accept None for globals and locals."""
776+
code = textwrap.dedent("""
777+
import sys
778+
import types
779+
780+
lazy = __lazy_import__("test.test_lazy_import.data.basic2",
781+
globals=None, locals=None)
782+
assert type(globals()["lazy"]) is types.LazyImportType
783+
assert "test.test_lazy_import.data.basic2" not in sys.modules
784+
""")
785+
assert_python_ok("-c", code)
786+
773787
def test_dunder_lazy_import_builtins(self):
774788
"""__lazy_import__ should use module's __builtins__ for __import__."""
775789
from test.test_lazy_import.data import dunder_lazy_import_builtins
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:func:`!__lazy_import__` now accepts ``None`` for *globals* and *locals*,
2+
matching its signature and :func:`__import__`.

‎Python/bltinmodule.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ builtin___lazy_import___impl(PyObject *module, PyObject *name,
312312
{
313313
PyObject *builtins;
314314
PyThreadState *tstate = PyThreadState_GET();
315-
if (globals == NULL) {
315+
if (globals == NULL || globals == Py_None) {
316316
globals = PyEval_GetGlobals();
317317
if (globals == NULL) {
318318
PyErr_SetString(PyExc_TypeError,
@@ -321,7 +321,7 @@ builtin___lazy_import___impl(PyObject *module, PyObject *name,
321321
return NULL;
322322
}
323323
}
324-
if (locals == NULL) {
324+
if (locals == NULL || locals == Py_None) {
325325
locals = globals;
326326
}
327327

0 commit comments

Comments
 (0)