bpo-30860: Fix a refleak. (#3567) · python/cpython@dae0276
@@ -36,12 +36,14 @@ extern const char *PyWin_DLLVersionString;
36363737_Py_IDENTIFIER(_);
3838_Py_IDENTIFIER(__sizeof__);
39+_Py_IDENTIFIER(_xoptions);
3940_Py_IDENTIFIER(buffer);
4041_Py_IDENTIFIER(builtins);
4142_Py_IDENTIFIER(encoding);
4243_Py_IDENTIFIER(path);
4344_Py_IDENTIFIER(stdout);
4445_Py_IDENTIFIER(stderr);
46+_Py_IDENTIFIER(warnoptions);
4547_Py_IDENTIFIER(write);
46484749PyObject *
@@ -1481,21 +1483,25 @@ list_builtin_module_names(void)
14811483static PyObject *
14821484get_warnoptions(void)
14831485{
1484-PyObject *warnoptions = PyThreadState_GET()->interp->warnoptions;
1486+PyObject *warnoptions = _PySys_GetObjectId(&PyId_warnoptions);
14851487if (warnoptions == NULL || !PyList_Check(warnoptions)) {
14861488Py_XDECREF(warnoptions);
14871489warnoptions = PyList_New(0);
14881490if (warnoptions == NULL)
14891491return NULL;
1490-PyThreadState_GET()->interp->warnoptions = warnoptions;
1492+if (_PySys_SetObjectId(&PyId_warnoptions, warnoptions)) {
1493+Py_DECREF(warnoptions);
1494+return NULL;
1495+ }
1496+Py_DECREF(warnoptions);
14911497 }
14921498return warnoptions;
14931499}
1494150014951501void
14961502PySys_ResetWarnOptions(void)
14971503{
1498-PyObject *warnoptions = PyThreadState_GET()->interp->warnoptions;
1504+PyObject *warnoptions = _PySys_GetObjectId(&PyId_warnoptions);
14991505if (warnoptions == NULL || !PyList_Check(warnoptions))
15001506return;
15011507PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL);
@@ -1524,20 +1530,24 @@ PySys_AddWarnOption(const wchar_t *s)
15241530int
15251531PySys_HasWarnOptions(void)
15261532{
1527-PyObject *warnoptions = PyThreadState_GET()->interp->warnoptions;
1533+PyObject *warnoptions = _PySys_GetObjectId(&PyId_warnoptions);
15281534return (warnoptions != NULL && (PyList_Size(warnoptions) > 0)) ? 1 : 0;
15291535}
1530153615311537static PyObject *
15321538get_xoptions(void)
15331539{
1534-PyObject *xoptions = PyThreadState_GET()->interp->xoptions;
1540+PyObject *xoptions = _PySys_GetObjectId(&PyId__xoptions);
15351541if (xoptions == NULL || !PyDict_Check(xoptions)) {
15361542Py_XDECREF(xoptions);
15371543xoptions = PyDict_New();
15381544if (xoptions == NULL)
15391545return NULL;
1540-PyThreadState_GET()->interp->xoptions = xoptions;
1546+if (_PySys_SetObjectId(&PyId__xoptions, xoptions)) {
1547+Py_DECREF(xoptions);
1548+return NULL;
1549+ }
1550+Py_DECREF(xoptions);
15411551 }
15421552return xoptions;
15431553}
@@ -2086,16 +2096,6 @@ _PySys_BeginInit(void)
20862096#undef SET_SYS_FROM_STRING_BORROW
2087209720882098/* Updating the sys namespace, returning integer error codes */
2089-#define SET_SYS_FROM_STRING_BORROW_INT_RESULT(key, value) \
2090- do { \
2091- PyObject *v = (value); \
2092- if (v == NULL) \
2093- return -1; \
2094- res = PyDict_SetItemString(sysdict, key, v); \
2095- if (res < 0) { \
2096- return res; \
2097- } \
2098- } while (0)
20992099#define SET_SYS_FROM_STRING_INT_RESULT(key, value) \
21002100 do { \
21012101 PyObject *v = (value); \
@@ -2140,23 +2140,18 @@ _PySys_EndInit(PyObject *sysdict)
21402140SET_SYS_FROM_STRING_INT_RESULT("base_exec_prefix",
21412141PyUnicode_FromWideChar(Py_GetExecPrefix(), -1));
214221422143-PyObject *warnoptions = get_warnoptions();
2144-if (warnoptions == NULL)
2143+if (get_warnoptions() == NULL)
21452144return -1;
2146-SET_SYS_FROM_STRING_BORROW_INT_RESULT("warnoptions", warnoptions);
214721452148-PyObject *xoptions = get_xoptions();
2149-if (xoptions == NULL)
2146+if (get_xoptions() == NULL)
21502147return -1;
2151-SET_SYS_FROM_STRING_BORROW_INT_RESULT("_xoptions", xoptions);
2152214821532149if (PyErr_Occurred())
21542150return -1;
21552151return 0;
21562152}
2157215321582154#undef SET_SYS_FROM_STRING_INT_RESULT
2159-#undef SET_SYS_FROM_STRING_BORROW_INT_RESULT
2160215521612156static PyObject *
21622157makepathobject(const wchar_t *path, wchar_t delim)