-
-
Notifications
You must be signed in to change notification settings - Fork 35k
gh-151408: Fix bug with lazy importing a previously imported and removed module #151412
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,2 @@ | ||
| Fix lazy submodule imports so package submodules removed from | ||
| :data:`sys.modules` are properly re-imported. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3885,6 +3885,76 @@ _PyImport_ResolveName(PyThreadState *tstate, PyObject *name, | |
| return resolve_name(tstate, name, globals, level); | ||
| } | ||
|
|
||
| PyObject * | ||
| _PyImport_ResolveLazyImportFromAttr(PyThreadState *tstate, | ||
| PyObject *package_name, | ||
| PyObject *name, PyObject *attr) | ||
| { | ||
| /* Lazy from-imports repair evicted package submodules without | ||
| changing eager from-import behavior. */ | ||
| if (!PyModule_Check(attr) || package_name == NULL | ||
| || !PyUnicode_Check(package_name)) { | ||
| return Py_NewRef(attr); | ||
| } | ||
|
|
||
| PyObject *package = PyImport_GetModule(package_name); | ||
| if (package == NULL) { | ||
| if (_PyErr_Occurred(tstate)) { | ||
| return NULL; | ||
| } | ||
| return Py_NewRef(attr); | ||
| } | ||
|
|
||
| int is_package = PyObject_HasAttrWithError(package, &_Py_ID(__path__)); | ||
|
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. Nit: do we have a test for the non-package branch? We dropped |
||
| Py_DECREF(package); | ||
| if (is_package <= 0) { | ||
| if (is_package < 0) { | ||
| return NULL; | ||
| } | ||
| return Py_NewRef(attr); | ||
| } | ||
|
|
||
| PyObject *fullmodname = PyUnicode_FromFormat("%U.%U", package_name, name); | ||
| if (fullmodname == NULL) { | ||
| return NULL; | ||
| } | ||
|
|
||
| PyObject *attr_name; | ||
| if (PyObject_GetOptionalAttr(attr, &_Py_ID(__name__), &attr_name) < 0) { | ||
| Py_DECREF(fullmodname); | ||
| return NULL; | ||
| } | ||
|
|
||
| int matches = (attr_name != NULL && PyUnicode_Check(attr_name)) | ||
|
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.
|
||
| ? PyObject_RichCompareBool(attr_name, fullmodname, Py_EQ) | ||
| : 0; | ||
| Py_XDECREF(attr_name); | ||
| if (matches <= 0) { | ||
| Py_DECREF(fullmodname); | ||
| return matches < 0 ? NULL : Py_NewRef(attr); | ||
| } | ||
|
|
||
| PyObject *submod = import_get_module(tstate, fullmodname); | ||
|
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. This can return a module that is still initializing in another thread. We need the initialization wait and recheck from |
||
| if (submod == NULL && !_PyErr_Occurred(tstate)) { | ||
| PyObject *imported = PyImport_ImportModuleLevelObject( | ||
|
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. This bypasses the |
||
| fullmodname, NULL, NULL, NULL, 0); | ||
| if (imported == NULL) { | ||
| Py_DECREF(fullmodname); | ||
| return NULL; | ||
| } | ||
| Py_DECREF(imported); | ||
| submod = import_get_module(tstate, fullmodname); | ||
| } | ||
| Py_DECREF(fullmodname); | ||
| if (submod != 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.
|
||
| return submod; | ||
| } | ||
| if (_PyErr_Occurred(tstate)) { | ||
| return NULL; | ||
| } | ||
| return Py_NewRef(attr); | ||
| } | ||
|
|
||
| PyObject * | ||
| _PyImport_LoadLazyImportTstate(PyThreadState *tstate, PyObject *lazy_import) | ||
| { | ||
|
|
@@ -4004,7 +4074,20 @@ _PyImport_LoadLazyImportTstate(PyThreadState *tstate, PyObject *lazy_import) | |
|
|
||
| if (lz->lz_attr != NULL && PyUnicode_Check(lz->lz_attr)) { | ||
| PyObject *from = obj; | ||
| obj = _PyEval_ImportFrom(tstate, from, lz->lz_attr); | ||
| obj = NULL; | ||
| PyObject *attr; | ||
| if (PyObject_GetOptionalAttr(from, lz->lz_attr, &attr) < 0) { | ||
| Py_DECREF(from); | ||
| goto error; | ||
| } | ||
| if (attr != NULL) { | ||
| obj = _PyImport_ResolveLazyImportFromAttr( | ||
| tstate, lz->lz_from, lz->lz_attr, attr); | ||
| Py_DECREF(attr); | ||
| } | ||
| else { | ||
| obj = _PyEval_ImportFrom(tstate, from, lz->lz_attr); | ||
|
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. Small nit: on the not-found path we look up |
||
| } | ||
| Py_DECREF(from); | ||
| if (obj == NULL) { | ||
| goto error; | ||
|
|
||
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.
This re-import runs while executing the
lazy fromstatement, beforebaris accessed. We should keep a lazy proxy here and repair it during reification.