Skip to content

Commit 1e53bba

Browse files
committed
Issue #18408: handle PySys_GetObject() failure, raise a RuntimeError
1 parent 1b63493 commit 1e53bba

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

Modules/_pickle.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,8 +1361,10 @@ whichmodule(PyObject *global, PyObject *global_name)
13611361

13621362
search:
13631363
modules_dict = PySys_GetObject("modules");
1364-
if (modules_dict == NULL)
1364+
if (modules_dict == NULL) {
1365+
PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
13651366
return NULL;
1367+
}
13661368

13671369
i = 0;
13681370
module_name = NULL;
@@ -5542,8 +5544,10 @@ Unpickler_find_class(UnpicklerObject *self, PyObject *args)
55425544
}
55435545

55445546
modules_dict = PySys_GetObject("modules");
5545-
if (modules_dict == NULL)
5547+
if (modules_dict == NULL) {
5548+
PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
55465549
return NULL;
5550+
}
55475551

55485552
module = PyDict_GetItemWithError(modules_dict, module_name);
55495553
if (module == NULL) {

Modules/main.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,10 @@ RunMainFromImporter(wchar_t *filename)
260260
/* argv0 is usable as an import source, so put it in sys.path[0]
261261
and import __main__ */
262262
sys_path = PySys_GetObject("path");
263-
if (sys_path == NULL)
263+
if (sys_path == NULL) {
264+
PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path");
264265
goto error;
266+
}
265267
if (PyList_SetItem(sys_path, 0, argv0)) {
266268
argv0 = NULL;
267269
goto error;

Python/bltinmodule.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,6 +1550,11 @@ builtin_print(PyObject *self, PyObject *args, PyObject *kwds)
15501550
return NULL;
15511551
if (file == NULL || file == Py_None) {
15521552
file = PySys_GetObject("stdout");
1553+
if (file == NULL) {
1554+
PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
1555+
return NULL;
1556+
}
1557+
15531558
/* sys.stdout may be None when FILE* stdout isn't connected */
15541559
if (file == Py_None)
15551560
Py_RETURN_NONE;

Python/import.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,10 @@ _PyImportZip_Init(void)
8585
int err = 0;
8686

8787
path_hooks = PySys_GetObject("path_hooks");
88-
if (path_hooks == NULL)
88+
if (path_hooks == NULL) {
89+
PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path_hooks");
8990
goto error;
91+
}
9092

9193
if (Py_VerboseFlag)
9294
PySys_WriteStderr("# installing zipimport hook\n");
@@ -944,11 +946,11 @@ PyAPI_FUNC(PyObject *)
944946
PyImport_GetImporter(PyObject *path) {
945947
PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL;
946948

947-
if ((path_importer_cache = PySys_GetObject("path_importer_cache"))) {
948-
if ((path_hooks = PySys_GetObject("path_hooks"))) {
949-
importer = get_path_importer(path_importer_cache,
950-
path_hooks, path);
951-
}
949+
path_importer_cache = PySys_GetObject("path_importer_cache");
950+
path_hooks = PySys_GetObject("path_hooks");
951+
if (path_importer_cache != NULL && path_hooks != NULL) {
952+
importer = get_path_importer(path_importer_cache,
953+
path_hooks, path);
952954
}
953955
Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */
954956
return importer;

0 commit comments

Comments
 (0)