Repository navigation
gh-162415: Use binding globals for lazy import hooks #162416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we describe the |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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__), | ||
|
|
@@ -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. | ||
|
|
@@ -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; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A dict subclass can return a proxy from 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 |
||
| PyObject *value = lazy_import_resolve_impl( | ||
| tstate, placeholder, &imported_module); | ||
| tstate, placeholder, &imported_module, globals); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about |
||
| if (value == NULL) { | ||
| Py_XDECREF(imported_module); | ||
| return NULL; | ||
|
|
||
There was a problem hiding this comment.
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()fromconsumerfirst? Check that both hook arguments areconsumerand that the owner still holds the same proxy. Then accessowner.targetand check that both arguments areowner.__dict__.