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);

46484749

PyObject *

@@ -1481,21 +1483,25 @@ list_builtin_module_names(void)

14811483

static PyObject *

14821484

get_warnoptions(void)

14831485

{

1484-

PyObject *warnoptions = PyThreadState_GET()->interp->warnoptions;

1486+

PyObject *warnoptions = _PySys_GetObjectId(&PyId_warnoptions);

14851487

if (warnoptions == NULL || !PyList_Check(warnoptions)) {

14861488

Py_XDECREF(warnoptions);

14871489

warnoptions = PyList_New(0);

14881490

if (warnoptions == NULL)

14891491

return 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

}

14921498

return warnoptions;

14931499

}

1494150014951501

void

14961502

PySys_ResetWarnOptions(void)

14971503

{

1498-

PyObject *warnoptions = PyThreadState_GET()->interp->warnoptions;

1504+

PyObject *warnoptions = _PySys_GetObjectId(&PyId_warnoptions);

14991505

if (warnoptions == NULL || !PyList_Check(warnoptions))

15001506

return;

15011507

PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL);

@@ -1524,20 +1530,24 @@ PySys_AddWarnOption(const wchar_t *s)

15241530

int

15251531

PySys_HasWarnOptions(void)

15261532

{

1527-

PyObject *warnoptions = PyThreadState_GET()->interp->warnoptions;

1533+

PyObject *warnoptions = _PySys_GetObjectId(&PyId_warnoptions);

15281534

return (warnoptions != NULL && (PyList_Size(warnoptions) > 0)) ? 1 : 0;

15291535

}

1530153615311537

static PyObject *

15321538

get_xoptions(void)

15331539

{

1534-

PyObject *xoptions = PyThreadState_GET()->interp->xoptions;

1540+

PyObject *xoptions = _PySys_GetObjectId(&PyId__xoptions);

15351541

if (xoptions == NULL || !PyDict_Check(xoptions)) {

15361542

Py_XDECREF(xoptions);

15371543

xoptions = PyDict_New();

15381544

if (xoptions == NULL)

15391545

return 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

}

15421552

return 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)

21402140

SET_SYS_FROM_STRING_INT_RESULT("base_exec_prefix",

21412141

PyUnicode_FromWideChar(Py_GetExecPrefix(), -1));

214221422143-

PyObject *warnoptions = get_warnoptions();

2144-

if (warnoptions == NULL)

2143+

if (get_warnoptions() == NULL)

21452144

return -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)

21502147

return -1;

2151-

SET_SYS_FROM_STRING_BORROW_INT_RESULT("_xoptions", xoptions);

2152214821532149

if (PyErr_Occurred())

21542150

return -1;

21552151

return 0;

21562152

}

2157215321582154

#undef SET_SYS_FROM_STRING_INT_RESULT

2159-

#undef SET_SYS_FROM_STRING_BORROW_INT_RESULT

2160215521612156

static PyObject *

21622157

makepathobject(const wchar_t *path, wchar_t delim)