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

@ -1236,20 +1236,22 @@ type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context)
}
else {
abstract = 0;
res = PyDict_DelItem(dict, &_Py_ID(__abstractmethods__));
if (res && PyErr_ExceptionMatches(PyExc_KeyError)) {
res = PyDict_Pop(dict, &_Py_ID(__abstractmethods__), NULL);
if (res == 0) {
PyErr_SetObject(PyExc_AttributeError, &_Py_ID(__abstractmethods__));
return -1;
}
}
if (res == 0) {
PyType_Modified(type);
if (abstract)
type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT;
else
type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT;
if (res < 0) {
return -1;
}
return res;
PyType_Modified(type);
if (abstract)
type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT;
else
type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT;
return 0;
}
static PyObject *
@ -1606,16 +1608,18 @@ type_set_annotations(PyTypeObject *type, PyObject *value, void *context)
result = PyDict_SetItem(dict, &_Py_ID(__annotations__), value);
} else {
/* delete */
result = PyDict_DelItem(dict, &_Py_ID(__annotations__));
if (result < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) {
result = PyDict_Pop(dict, &_Py_ID(__annotations__), NULL);
if (result == 0) {
PyErr_SetString(PyExc_AttributeError, "__annotations__");
return -1;
}
}
if (result == 0) {
PyType_Modified(type);
if (result < 0) {
return -1;
}
return result;
PyType_Modified(type);
return 0;
}
static PyObject *