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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we call owner.__dict__['target'].resolve() from consumer first? Check that both hook arguments are consumer and that the owner still holds the same proxy. Then access owner.target and check that both arguments are owner.__dict__.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we describe the owner.target case directly? Two automatic paths still use caller globals. One resolves a raw proxy returned by an import hook. The other loads a pending child such as xml.dom through the consumer’s reference to xml.

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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A dict subclass can return a proxy from __missing__. With a fresh owner from the new test, this passes on the parent and fails on this branch.

class Locals(dict):
    def __missing__(self, name):
        return owner.__dict__[name]

eval('target', owner.__dict__, Locals())
assert calls == [(True, True)], calls

The hook gets the empty Locals instance. A property on a module subclass can also return a proxy from another module. Can we keep the caller fallback when the lookup cannot identify an owner?

PyObject *value = lazy_import_resolve_impl(
tstate, placeholder, &imported_module);
tstate, placeholder, &imported_module, globals);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about frozendict? Put the proxy in the consumer’s builtins and evaluate (lambda: target)(). With a dict, the hook gets that dict. With a frozendict, it gets the consumer globals. Both contain the entry. Can we cover both and check that the frozen dict still holds the proxy after access?

if (value == NULL) {
Py_XDECREF(imported_module);
return NULL;
Expand Down
Loading