Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Lib/test/test_lazy_import/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:func:`!__lazy_import__` now treats ``None`` for *globals* and *locals* the
same as omitting them, matching its signature.
4 changes: 2 additions & 2 deletions Python/bltinmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
}

Expand Down
Loading