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
24 changes: 24 additions & 0 deletions Lib/test/test_lazy_import/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,30 @@ def denied(*args):
exec('assert xml.dom.__name__ == "xml.dom"', first)
""")

def test_import_hook_receives_binding_globals(self):
self.check("""
import builtins, types
owner = types.ModuleType('owner')
calls = []
def import_hook(
name, globals=None, locals=None, fromlist=(), level=0,
):
calls.append((globals is owner.__dict__, locals is globals))
module = types.ModuleType(name)
module.value = 42
return module
owner.__dict__['__builtins__'] = dict(
vars(builtins), __import__=import_hook)
exec('lazy import target', owner.__dict__)
consumer = {
'__name__': 'consumer',
'__builtins__': vars(builtins),
'owner': owner,
}
exec('assert owner.target.value == 42', consumer)
assert calls == [(True, True)], calls
""")

def test_retry_after_recursive_access(self):
self.check("""
import sys, xml
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use the globals that own a lazy import binding when reifying it.
13 changes: 8 additions & 5 deletions Objects/lazyimportobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ _PyLazyImport_IsResolving(PyThreadState *tstate, PyObject *op)

static PyObject *
lazy_import_resolve_impl(PyThreadState *tstate, PyObject *lazy_import,
PyObject **imported_module)
PyObject **imported_module, PyObject *globals)
{
PyObject *obj = NULL;
PyObject *fromlist = NULL;
Expand Down Expand Up @@ -428,9 +428,11 @@ lazy_import_resolve_impl(PyThreadState *tstate, PyObject *lazy_import,
goto done;
}

PyObject *globals = PyEval_GetGlobals();
if (globals == NULL) {
globals = Py_None;
globals = PyEval_GetGlobals();
if (globals == NULL) {
globals = Py_None;
}
}

if (PyMapping_GetOptionalItem(root->lz_builtins, &_Py_ID(__import__),
Expand Down Expand Up @@ -564,7 +566,7 @@ lazy_import_resolve_impl(PyThreadState *tstate, PyObject *lazy_import,
PyObject *
_PyImport_LoadLazyImportTstate(PyThreadState *tstate, PyObject *lazy_import)
{
return lazy_import_resolve_impl(tstate, lazy_import, NULL);
return lazy_import_resolve_impl(tstate, lazy_import, NULL, NULL);
}

// Resolve a pending child as an aliased import, preserving its declaration.
Expand Down Expand Up @@ -671,8 +673,9 @@ _PyLazyImport_Reify(PyThreadState *tstate, PyObject *placeholder,
PyObject *name, PyObject *namespace)
{
PyObject *imported_module = NULL;
PyObject *globals = PyDict_Check(namespace) ? namespace : NULL;
PyObject *value = lazy_import_resolve_impl(
tstate, placeholder, &imported_module);
tstate, placeholder, &imported_module, globals);
if (value == NULL) {
Py_XDECREF(imported_module);
return NULL;
Expand Down
Loading