Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5804960

Browse files
committed
Issue #18520: Fix _PySys_Init(), handle PyDict_SetItemString() errors
1 parent f54a574 commit 5804960

1 file changed

Lines changed: 21 additions & 17 deletions

File tree

Python/sysmodule.c

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,17 +1565,24 @@ static struct PyModuleDef sysmodule = {
15651565
PyObject *
15661566
_PySys_Init(void)
15671567
{
1568-
PyObject *m, *v, *sysdict, *version_info;
1568+
PyObject *m, *sysdict, *version_info;
15691569

15701570
m = PyModule_Create(&sysmodule);
15711571
if (m == NULL)
15721572
return NULL;
15731573
sysdict = PyModule_GetDict(m);
1574-
#define SET_SYS_FROM_STRING(key, value) \
1575-
v = value; \
1576-
if (v != NULL) \
1577-
PyDict_SetItemString(sysdict, key, v); \
1578-
Py_XDECREF(v)
1574+
#define SET_SYS_FROM_STRING(key, value) \
1575+
do { \
1576+
int res; \
1577+
PyObject *v = (value); \
1578+
if (v == NULL) \
1579+
return NULL; \
1580+
res = PyDict_SetItemString(sysdict, key, v); \
1581+
if (res < 0) { \
1582+
Py_DECREF(v); \
1583+
return NULL; \
1584+
} \
1585+
} while (0)
15791586

15801587
/* Check that stdin is not a directory
15811588
Using shell redirection, you can redirect stdin to a directory,
@@ -1597,10 +1604,10 @@ _PySys_Init(void)
15971604

15981605
/* stdin/stdout/stderr are now set by pythonrun.c */
15991606

1600-
PyDict_SetItemString(sysdict, "__displayhook__",
1601-
PyDict_GetItemString(sysdict, "displayhook"));
1602-
PyDict_SetItemString(sysdict, "__excepthook__",
1603-
PyDict_GetItemString(sysdict, "excepthook"));
1607+
SET_SYS_FROM_STRING("__displayhook__",
1608+
PyDict_GetItemString(sysdict, "displayhook"));
1609+
SET_SYS_FROM_STRING("__excepthook__",
1610+
PyDict_GetItemString(sysdict, "excepthook"));
16041611
SET_SYS_FROM_STRING("version",
16051612
PyUnicode_FromString(Py_GetVersion()));
16061613
SET_SYS_FROM_STRING("hexversion",
@@ -1664,18 +1671,15 @@ _PySys_Init(void)
16641671
#endif
16651672
if (warnoptions == NULL) {
16661673
warnoptions = PyList_New(0);
1674+
if (warnoptions == NULL)
1675+
return NULL;
16671676
}
16681677
else {
16691678
Py_INCREF(warnoptions);
16701679
}
1671-
if (warnoptions != NULL) {
1672-
PyDict_SetItemString(sysdict, "warnoptions", warnoptions);
1673-
}
1680+
SET_SYS_FROM_STRING("warnoptions", warnoptions);
16741681

1675-
v = get_xoptions();
1676-
if (v != NULL) {
1677-
PyDict_SetItemString(sysdict, "_xoptions", v);
1678-
}
1682+
SET_SYS_FROM_STRING("_xoptions", get_xoptions());
16791683

16801684
/* version_info */
16811685
if (VersionInfoType.tp_name == NULL) {

0 commit comments

Comments
 (0)