gh-116437: Use new C API PyDict_Pop() to simplify the code (GH-116438)

This commit is contained in:
Serhiy Storchaka 2024-03-07 11:21:08 +02:00 committed by GitHub
parent 882fcede83
commit 72d3cc94cd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 116 additions and 119 deletions

View file

@ -372,33 +372,27 @@ element_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
static PyObject*
get_attrib_from_keywords(PyObject *kwds)
{
PyObject *attrib_str = PyUnicode_FromString("attrib");
if (attrib_str == NULL) {
PyObject *attrib;
if (PyDict_PopString(kwds, "attrib", &attrib) < 0) {
return NULL;
}
PyObject *attrib = PyDict_GetItemWithError(kwds, attrib_str);
if (attrib) {
/* If attrib was found in kwds, copy its value and remove it from
* kwds
*/
if (!PyDict_Check(attrib)) {
Py_DECREF(attrib_str);
PyErr_Format(PyExc_TypeError, "attrib must be dict, not %.100s",
Py_TYPE(attrib)->tp_name);
Py_DECREF(attrib);
return NULL;
}
attrib = PyDict_Copy(attrib);
if (attrib && PyDict_DelItem(kwds, attrib_str) < 0) {
Py_SETREF(attrib, NULL);
}
Py_SETREF(attrib, PyDict_Copy(attrib));
}
else if (!PyErr_Occurred()) {
else {
attrib = PyDict_New();
}
Py_DECREF(attrib_str);
if (attrib != NULL && PyDict_Update(attrib, kwds) < 0) {
Py_DECREF(attrib);
return NULL;