Skip to content

[3.11] gh-109521: Fix obscure cases handling in PyImport_GetImporter() (GH-109522) #109781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
:c:func:`PyImport_GetImporter` now sets RuntimeError if it fails to get
:data:`sys.path_hooks` or :data:`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.
15 changes: 13 additions & 2 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down