Add PyExc_ImportCycleError and raise it when a cycle is detected

This commit is contained in:
Dino Viehland 2025-09-29 10:19:34 -07:00
parent 00e7800e4c
commit 781eedb9d4
9 changed files with 57 additions and 15 deletions

View file

@ -1046,6 +1046,13 @@ _Py_module_getattro_impl(PyModuleObject *m, PyObject *name, int suppress)
if (PyLazyImport_CheckExact(attr)) {
PyObject *new_value = _PyImport_LoadLazyImportTstate(PyThreadState_GET(), attr);
if (new_value == NULL) {
if (suppress && PyErr_ExceptionMatches(PyExc_ImportCycleError)) {
// ImportCycleError is raised when a lazy object tries to import itself.
// In this case, the error should not propagate to the caller and
// instead treated as if the attribute doesn't exist.
PyErr_Clear();
}
return NULL;
} else if (PyDict_SetItem(m->md_dict, name, new_value) < 0) {
Py_DECREF(new_value);