gh-108308: Replace _PyDict_GetItemStringWithError() (#108372)

Replace _PyDict_GetItemStringWithError() calls with
PyDict_GetItemStringRef() which returns a strong reference to the
item.

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
Victor Stinner 2023-08-23 22:59:00 +02:00 committed by GitHub
parent 1700d34d31
commit 4dc9f48930
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 39 deletions

View file

@ -8,7 +8,6 @@
*/
#include "Python.h"
#include "pycore_dict.h" // _PyDict_GetItemStringWithError()
#include "pycore_tuple.h" // _PyTuple_FromArray()
#include "pycore_object.h" // _PyObject_GC_TRACK()
@ -149,7 +148,6 @@ static PyObject *
structseq_new_impl(PyTypeObject *type, PyObject *arg, PyObject *dict)
/*[clinic end generated code: output=baa082e788b171da input=90532511101aa3fb]*/
{
PyObject *ob;
PyStructSequence *res = NULL;
Py_ssize_t len, min_len, max_len, i, n_unnamed_fields;
@ -219,21 +217,18 @@ structseq_new_impl(PyTypeObject *type, PyObject *arg, PyObject *dict)
}
Py_DECREF(arg);
for (; i < max_len; ++i) {
if (dict == NULL) {
ob = Py_None;
}
else {
ob = _PyDict_GetItemStringWithError(dict,
type->tp_members[i-n_unnamed_fields].name);
if (ob == NULL) {
if (PyErr_Occurred()) {
Py_DECREF(res);
return NULL;
}
ob = Py_None;
PyObject *ob = NULL;
if (dict != NULL) {
const char *name = type->tp_members[i-n_unnamed_fields].name;
if (PyDict_GetItemStringRef(dict, name, &ob) < 0) {
Py_DECREF(res);
return NULL;
}
}
res->ob_item[i] = Py_NewRef(ob);
if (ob == NULL) {
ob = Py_NewRef(Py_None);
}
res->ob_item[i] = ob;
}
_PyObject_GC_TRACK(res);