diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2023-10-07 13:05:13 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-07 13:05:13 (GMT) |
commit | 6a33529cf0bcfe9001af58e45ea8feab4daed265 (patch) | |
tree | 054d9860d99141dbf7c02c0dcbeceac194e412fe /Python | |
parent | 3d5aa7ec6129330c10e76435b627ff1df2cb08f7 (diff) | |
download | cpython-6a33529cf0bcfe9001af58e45ea8feab4daed265.zip cpython-6a33529cf0bcfe9001af58e45ea8feab4daed265.tar.gz cpython-6a33529cf0bcfe9001af58e45ea8feab4daed265.tar.bz2 |
[3.11] gh-109521: Fix obscure cases handling in PyImport_GetImporter() (GH-109522) (GH-109781)
PyImport_GetImporter() now sets RuntimeError if it fails to get sys.path_hooks
or sys.path_importer_cache or they are not list and dict correspondingly.
Previously it could return NULL without setting error in obscure cases,
crash or raise SystemError if these attributes have wrong type.
(cherry picked from commit 62c7015e89cbdedb5218d4fedd45f971885f67a8)
Diffstat (limited to 'Python')
-rw-r--r-- | Python/import.c | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/Python/import.c b/Python/import.c index 38c23e0..39144d3 100644 --- a/Python/import.c +++ b/Python/import.c @@ -951,11 +951,22 @@ PyImport_GetImporter(PyObject *path) { PyThreadState *tstate = _PyThreadState_GET(); PyObject *path_importer_cache = PySys_GetObject("path_importer_cache"); + if (path_importer_cache == NULL) { + PyErr_SetString(PyExc_RuntimeError, "lost sys.path_importer_cache"); + return NULL; + } + Py_INCREF(path_importer_cache); PyObject *path_hooks = PySys_GetObject("path_hooks"); - if (path_importer_cache == NULL || path_hooks == NULL) { + if (path_hooks == NULL) { + PyErr_SetString(PyExc_RuntimeError, "lost sys.path_hooks"); + Py_DECREF(path_importer_cache); return NULL; } - return get_path_importer(tstate, path_importer_cache, path_hooks, path); + Py_INCREF(path_hooks); + PyObject *importer = get_path_importer(tstate, path_importer_cache, path_hooks, path); + Py_DECREF(path_hooks); + Py_DECREF(path_importer_cache); + return importer; } #if defined(__EMSCRIPTEN__) && defined(PY_CALL_TRAMPOLINE) |