aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Python/import.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2023-12-07 12:19:43 +0200
committerGitHub <noreply@github.com>2023-12-07 12:19:43 +0200
commit8660fb7fd7cdcbfe58ef304f5720efe97ca7c842 (patch)
tree17cd5657cd0c6cc5795d59973a1a572a9274d60c /Python/import.c
parent953ee622b3901d3467e65e3484dcfa75ba6fcddf (diff)
downloadcpython-8660fb7fd7cdcbfe58ef304f5720efe97ca7c842.tar.gz
cpython-8660fb7fd7cdcbfe58ef304f5720efe97ca7c842.zip
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.
Diffstat (limited to 'Python/import.c')
-rw-r--r--Python/import.c25
1 files changed, 14 insertions, 11 deletions
diff --git a/Python/import.c b/Python/import.c
index f37393bbdc4..ef81f46a4d6 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -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;
}