gh-112660: Do not clear arbitrary errors on import (GH-112661)

Previously arbitrary errors could be cleared during formatting error
messages for ImportError or AttributeError for modules. Now all
unexpected errors are reported.
This commit is contained in:
Serhiy Storchaka 2023-12-07 12:19:43 +02:00 committed by GitHub
parent 953ee622b3
commit 8660fb7fd7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 72 additions and 58 deletions

View file

@ -252,18 +252,21 @@ import_ensure_initialized(PyInterpreterState *interp, PyObject *mod, PyObject *n
NOTE: because of this, initializing must be set *before*
stuffing the new module in sys.modules.
*/
spec = PyObject_GetAttr(mod, &_Py_ID(__spec__));
int busy = _PyModuleSpec_IsInitializing(spec);
Py_XDECREF(spec);
if (busy) {
/* Wait until module is done importing. */
PyObject *value = PyObject_CallMethodOneArg(
IMPORTLIB(interp), &_Py_ID(_lock_unlock_module), name);
if (value == NULL) {
return -1;
}
Py_DECREF(value);
int rc = PyObject_GetOptionalAttr(mod, &_Py_ID(__spec__), &spec);
if (rc > 0) {
rc = _PyModuleSpec_IsInitializing(spec);
Py_DECREF(spec);
}
if (rc <= 0) {
return rc;
}
/* Wait until module is done importing. */
PyObject *value = PyObject_CallMethodOneArg(
IMPORTLIB(interp), &_Py_ID(_lock_unlock_module), name);
if (value == NULL) {
return -1;
}
Py_DECREF(value);
return 0;
}