8000 gh-108308: config_dict_get() uses PyDict_GetItemRef() by vstinner · Pull Request #108371 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 23, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gh-108308: config_dict_get() uses PyDict_GetItemRef()
Replace _PyDict_GetItemStringWithError() with PyDict_GetItemRef() in
config_dict_get() to get a strong reference to the item.
  • Loading branch information
vstinner committed Aug 23, 2023
commit 001b0df865223389dd54dc77dbbca561e6b6c9af
24 changes: 19 additions & 5 deletions Python/initconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -1065,8 +1065,11 @@ _PyConfig_AsDict(const PyConfig *config)
static PyObject*
config_dict_get(PyObject *dict, const char *name)
{
PyObject *item = _PyDict_GetItemStringWithError(dict, name);
if (item == NULL && !PyErr_Occurred()) {
PyObject *item;
if (PyDict_GetItemStringRef(dict, name, &item) < 0) {
return NULL;
}
if (item == NULL) {
PyErr_Format(PyExc_ValueError, "missing config key: %s", name);
return NULL;
}
Expand Down Expand Up @@ -1096,6 +1099,7 @@ config_dict_get_int(PyObject *dict, const char *name, int *result)
return -1;
}
int value = _PyLong_AsInt(item);
Py_DECREF(item);
if (value == -1 && PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_TypeError)) {
config_dict_invalid_type(name);
Expand All @@ -1118,6 +1122,7 @@ config_dict_get_ulong(PyObject *dict, const char *name, unsigned long *result)
return -1;
}
unsigned long value = PyLong_AsUnsignedLong(item);
Py_DECREF(item);
if (value == (unsigned long)-1 && PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_TypeError)) {
config_dict_invalid_type(name);
Expand All @@ -1140,27 +1145,33 @@ config_dict_get_wstr(PyObject *dict, const char *name, PyConfig *config,
if (item == NULL) {
return -1;
}

PyStatus status;
if (item == Py_None) {
status = PyConfig_SetString(config, result, NULL);
}
else if (!PyUnicode_Check(item)) {
config_dict_invalid_type(name);
return -1;
goto error;
}
else {
wchar_t *wstr = PyUnicode_AsWideCharString(item, NULL);
if (wstr == NULL) {
return -1;
goto error;
}
status = PyConfig_SetString(config, result, wstr);
PyMem_Free(wstr);
}
if (_PyStatus_EXCEPTION(status)) {
PyErr_NoMemory();
return -1;
goto error;
}
Py_DECREF(item);
return 0;

error:
Py_DECREF(item);
return -1;
}


Expand All @@ -1174,6 +1185,7 @@ config_dict_get_wstrlist(PyObject *dict, const char *name, PyConfig *config,
}

if (!PyList_CheckExact(list)) {
Py_DECREF(list);
config_dict_invalid_type(name);
return -1;
}
Expand Down Expand Up @@ -1207,10 +1219,12 @@ config_dict_get_wstrlist(PyObject *dict, const char *name, PyConfig *config,
goto error;
}
_PyWideStringList_Clear(&wstrlist);
Py_DECREF(list);
return 0;

error:
_PyWideStringList_Clear(&wstrlist);
Py_DECREF(list);
return -1;
}

< 4160 /span>
Expand Down
0