Skip to content

gh-162415: Use binding globals for lazy import hooks - #162416

Open
tonghuaroot wants to merge 1 commit into
python:mainfrom
tonghuaroot:pep810-declaration-globals
Open

tonghuaroot wants to merge 1 commit into
python:mainfrom
tonghuaroot:pep810-declaration-globals

Conversation

@tonghuaroot

@tonghuaroot tonghuaroot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Use the namespace that owns a lazy import binding as the globals and locals
passed to the saved import function during automatic reification. Explicit
resolve() calls keep their existing caller-globals behavior.

Automatic reification could pass the accessing module's globals to a custom import function. Use the namespace that owns the lazy binding instead.

@pablogsal pablogsal left a comment

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.

This fixes owner.target. Can we pass the hook globals from the lookup that found the proxy? The namespace type alone cannot tell us who owns the name. I agree with keeping caller globals for explicit resolve().

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 *globals = PyDict_Check(namespace) ? namespace : NULL;
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?

'__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__.

@@ -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.

@bedevere-app

bedevere-app Bot commented Nov 24, 2026

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants