Skip to content

Commit 5e6e996

Browse files
[3.12] gh-109521: Fix obscure cases handling in PyImport_GetImporter() (GH-109522) (#109777)
gh-109521: Fix obscure cases handling in PyImport_GetImporter() (GH-109522) 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 62c7015) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 6a6bea3 commit 5e6e996

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
:c:func:`PyImport_GetImporter` now sets RuntimeError if it fails to get
2+
:data:`sys.path_hooks` or :data:`sys.path_importer_cache` or they are not
3+
list and dict correspondingly. Previously it could return NULL without
4+
setting error in obscure cases, crash or raise SystemError if these
5+
attributes have wrong type.

Python/import.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2380,9 +2380,14 @@ get_path_importer(PyThreadState *tstate, PyObject *path_importer_cache,
23802380
PyObject *importer;
23812381
Py_ssize_t j, nhooks;
23822382

2383-
/* These conditions are the caller's responsibility: */
2384-
assert(PyList_Check(path_hooks));
2385-
assert(PyDict_Check(path_importer_cache));
2383+
if (!PyList_Check(path_hooks)) {
2384+
PyErr_SetString(PyExc_RuntimeError, "sys.path_hooks is not a list");
2385+
return NULL;
2386+
}
2387+
if (!PyDict_Check(path_importer_cache)) {
2388+
PyErr_SetString(PyExc_RuntimeError, "sys.path_importer_cache is not a dict");
2389+
return NULL;
2390+
}
23862391

23872392
nhooks = PyList_Size(path_hooks);
23882393
if (nhooks < 0)
@@ -2425,11 +2430,22 @@ PyImport_GetImporter(PyObject *path)
24252430
{
24262431
PyThreadState *tstate = _PyThreadState_GET();
24272432
PyObject *path_importer_cache = PySys_GetObject("path_importer_cache");
2433+
if (path_importer_cache == NULL) {
2434+
PyErr_SetString(PyExc_RuntimeError, "lost sys.path_importer_cache");
2435+
return NULL;
2436+
}
2437+
Py_INCREF(path_importer_cache);
24282438
PyObject *path_hooks = PySys_GetObject("path_hooks");
2429-
if (path_importer_cache == NULL || path_hooks == NULL) {
2439+
if (path_hooks == NULL) {
2440+
PyErr_SetString(PyExc_RuntimeError, "lost sys.path_hooks");
2441+
Py_DECREF(path_importer_cache);
24302442
return NULL;
24312443
}
2432-
return get_path_importer(tstate, path_importer_cache, path_hooks, path);
2444+
Py_INCREF(path_hooks);
2445+
PyObject *importer = get_path_importer(tstate, path_importer_cache, path_hooks, path);
2446+
Py_DECREF(path_hooks);
2447+
Py_DECREF(path_importer_cache);
2448+
return importer;
24332449
}
24342450

24352451

0 commit comments

Comments
 (0)