Move eager check for from imports into import from

This commit is contained in:
Dino Viehland 2025-10-02 13:36:18 -07:00
parent 7d07ae13d7
commit c63198cba4
5 changed files with 37 additions and 9 deletions

View file

@ -3304,6 +3304,22 @@ _PyEval_LazyImportFrom(PyThreadState *tstate, PyObject *v, PyObject *name)
assert(name && PyUnicode_Check(name));
PyObject *ret;
PyLazyImportObject *d = (PyLazyImportObject *)v;
PyObject *mod = PyImport_GetModule(d->lz_from);
if (mod != NULL) {
// Check if the module already has the attribute, if so, resolve it eagerly.
if (PyModule_Check(mod)) {
PyObject *mod_dict = PyModule_GetDict(mod);
if (mod_dict != NULL) {
if (PyDict_GetItemRef(mod_dict, name, &ret) < 0) {
return NULL;
} else if (ret != NULL) {
return ret;
}
}
}
Py_DECREF(mod);
}
if (d->lz_attr != NULL) {
if (PyUnicode_Check(d->lz_attr)) {
PyObject *from = PyUnicode_FromFormat("%U.%U", d->lz_from, d->lz_attr);