summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2013-10-27 16:15:42 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2013-10-27 16:15:42 (GMT)
commit8fea252a507024edf00d5d98881d22dc8799a8d3 (patch)
treeb6f0f2557dcb5604c085af1e497375fec610605b /Python
parent22da9677c2dfa3c56bb5363ce353519b973d8842 (diff)
downloadcpython-8fea252a507024edf00d5d98881d22dc8799a8d3.zip
cpython-8fea252a507024edf00d5d98881d22dc8799a8d3.tar.gz
cpython-8fea252a507024edf00d5d98881d22dc8799a8d3.tar.bz2
Issue #18520: fix reference leak in _PySys_Init()
Diffstat (limited to 'Python')
-rw-r--r--Python/sysmodule.c25
1 files changed, 18 insertions, 7 deletions
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index 2d7e01b..d8848ae 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -1573,6 +1573,17 @@ _PySys_Init(void)
if (m == NULL)
return NULL;
sysdict = PyModule_GetDict(m);
+#define SET_SYS_FROM_STRING_BORROW(key, value) \
+ do { \
+ int res; \
+ PyObject *v = (value); \
+ if (v == NULL) \
+ return NULL; \
+ res = PyDict_SetItemString(sysdict, key, v); \
+ if (res < 0) { \
+ return NULL; \
+ } \
+ } while (0)
#define SET_SYS_FROM_STRING(key, value) \
do { \
int res; \
@@ -1580,8 +1591,8 @@ _PySys_Init(void)
if (v == NULL) \
return NULL; \
res = PyDict_SetItemString(sysdict, key, v); \
+ Py_DECREF(v); \
if (res < 0) { \
- Py_DECREF(v); \
return NULL; \
} \
} while (0)
@@ -1606,10 +1617,10 @@ _PySys_Init(void)
/* stdin/stdout/stderr are now set by pythonrun.c */
- SET_SYS_FROM_STRING("__displayhook__",
- PyDict_GetItemString(sysdict, "displayhook"));
- SET_SYS_FROM_STRING("__excepthook__",
- PyDict_GetItemString(sysdict, "excepthook"));
+ SET_SYS_FROM_STRING_BORROW("__displayhook__",
+ PyDict_GetItemString(sysdict, "displayhook"));
+ SET_SYS_FROM_STRING_BORROW("__excepthook__",
+ PyDict_GetItemString(sysdict, "excepthook"));
SET_SYS_FROM_STRING("version",
PyUnicode_FromString(Py_GetVersion()));
SET_SYS_FROM_STRING("hexversion",
@@ -1679,9 +1690,9 @@ _PySys_Init(void)
else {
Py_INCREF(warnoptions);
}
- SET_SYS_FROM_STRING("warnoptions", warnoptions);
+ SET_SYS_FROM_STRING_BORROW("warnoptions", warnoptions);
- SET_SYS_FROM_STRING("_xoptions", get_xoptions());
+ SET_SYS_FROM_STRING_BORROW("_xoptions", get_xoptions());
/* version_info */
if (VersionInfoType.tp_name == NULL) {