gh-108308: config_dict_get() uses PyDict_GetItemRef() (#108371)

Replace _PyDict_GetItemStringWithError() with PyDict_GetItemRef() in
config_dict_get() to get a strong reference to the item.
This commit is contained in:
Victor Stinner 2023-08-23 23:12:08 +02:00 committed by GitHub
parent 4dc9f48930
commit ec3527d196
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 6 deletions

View File

@ -1,5 +1,4 @@
#include "Python.h" #include "Python.h"
#include "pycore_dict.h" // _PyDict_GetItemStringWithError()
#include "pycore_fileutils.h" // _Py_HasFileSystemDefaultEncodeErrors #include "pycore_fileutils.h" // _Py_HasFileSystemDefaultEncodeErrors
#include "pycore_getopt.h" // _PyOS_GetOpt() #include "pycore_getopt.h" // _PyOS_GetOpt()
#include "pycore_initconfig.h" // _PyStatus_OK() #include "pycore_initconfig.h" // _PyStatus_OK()
@ -1065,8 +1064,11 @@ fail:
static PyObject* static PyObject*
config_dict_get(PyObject *dict, const char *name) config_dict_get(PyObject *dict, const char *name)
{ {
PyObject *item = _PyDict_GetItemStringWithError(dict, name); PyObject *item;
if (item == NULL && !PyErr_Occurred()) { if (PyDict_GetItemStringRef(dict, name, &item) < 0) {
return NULL;
}
if (item == NULL) {
PyErr_Format(PyExc_ValueError, "missing config key: %s", name); PyErr_Format(PyExc_ValueError, "missing config key: %s", name);
return NULL; return NULL;
} }
@ -1096,6 +1098,7 @@ config_dict_get_int(PyObject *dict, const char *name, int *result)
return -1; return -1;
} }
int value = _PyLong_AsInt(item); int value = _PyLong_AsInt(item);
Py_DECREF(item);
if (value == -1 && PyErr_Occurred()) { if (value == -1 && PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_TypeError)) { if (PyErr_ExceptionMatches(PyExc_TypeError)) {
config_dict_invalid_type(name); config_dict_invalid_type(name);
@ -1118,6 +1121,7 @@ config_dict_get_ulong(PyObject *dict, const char *name, unsigned long *result)
return -1; return -1;
} }
unsigned long value = PyLong_AsUnsignedLong(item); unsigned long value = PyLong_AsUnsignedLong(item);
Py_DECREF(item);
if (value == (unsigned long)-1 && PyErr_Occurred()) { if (value == (unsigned long)-1 && PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_TypeError)) { if (PyErr_ExceptionMatches(PyExc_TypeError)) {
config_dict_invalid_type(name); config_dict_invalid_type(name);
@ -1140,27 +1144,33 @@ config_dict_get_wstr(PyObject *dict, const char *name, PyConfig *config,
if (item == NULL) { if (item == NULL) {
return -1; return -1;
} }
PyStatus status; PyStatus status;
if (item == Py_None) { if (item == Py_None) {
status = PyConfig_SetString(config, result, NULL); status = PyConfig_SetString(config, result, NULL);
} }
else if (!PyUnicode_Check(item)) { else if (!PyUnicode_Check(item)) {
config_dict_invalid_type(name); config_dict_invalid_type(name);
return -1; goto error;
} }
else { else {
wchar_t *wstr = PyUnicode_AsWideCharString(item, NULL); wchar_t *wstr = PyUnicode_AsWideCharString(item, NULL);
if (wstr == NULL) { if (wstr == NULL) {
return -1; goto error;
} }
status = PyConfig_SetString(config, result, wstr); status = PyConfig_SetString(config, result, wstr);
PyMem_Free(wstr); PyMem_Free(wstr);
} }
if (_PyStatus_EXCEPTION(status)) { if (_PyStatus_EXCEPTION(status)) {
PyErr_NoMemory(); PyErr_NoMemory();
return -1; goto error;
} }
Py_DECREF(item);
return 0; return 0;
error:
Py_DECREF(item);
return -1;
} }
@ -1174,6 +1184,7 @@ config_dict_get_wstrlist(PyObject *dict, const char *name, PyConfig *config,
} }
if (!PyList_CheckExact(list)) { if (!PyList_CheckExact(list)) {
Py_DECREF(list);
config_dict_invalid_type(name); config_dict_invalid_type(name);
return -1; return -1;
} }
@ -1207,10 +1218,12 @@ config_dict_get_wstrlist(PyObject *dict, const char *name, PyConfig *config,
goto error; goto error;
} }
_PyWideStringList_Clear(&wstrlist); _PyWideStringList_Clear(&wstrlist);
Py_DECREF(list);
return 0; return 0;
error: error:
_PyWideStringList_Clear(&wstrlist); _PyWideStringList_Clear(&wstrlist);
Py_DECREF(list);
return -1; return -1;
} }