Repository navigation
gh-162415: Use binding globals for lazy import hooks - #162416
tonghuaroot wants to merge 1 commit into
Conversation
Automatic reification could pass the accessing module's globals to a custom import function. Use the namespace that owns the lazy binding instead.
pablogsal
left a comment
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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)], callsThe 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); |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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. | |||
There was a problem hiding this comment.
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.
|
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 |
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.