mirror of
https://github.com/python/cpython.git
synced 2025-10-03 05:35:59 +00:00
Refactor set.discard() and set.remove().
This commit is contained in:
parent
6a8bbdbe7b
commit
0deab62704
1 changed files with 22 additions and 31 deletions
|
@ -777,21 +777,19 @@ This has no effect if the element is already present.");
|
||||||
static PyObject *
|
static PyObject *
|
||||||
set_remove(PySetObject *so, PyObject *item)
|
set_remove(PySetObject *so, PyObject *item)
|
||||||
{
|
{
|
||||||
PyObject *tmp;
|
PyObject *tmp, *result;
|
||||||
|
|
||||||
if (PyDict_DelItem(so->data, item) == -1) {
|
if (PyType_IsSubtype(item->ob_type, &PySet_Type)) {
|
||||||
if (!PyType_IsSubtype(item->ob_type, &PySet_Type))
|
|
||||||
return NULL;
|
|
||||||
PyErr_Clear();
|
|
||||||
tmp = frozenset_dict_wrapper(((PySetObject *)(item))->data);
|
tmp = frozenset_dict_wrapper(((PySetObject *)(item))->data);
|
||||||
if (tmp == NULL)
|
if (tmp == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
if (PyDict_DelItem(so->data, tmp) == -1) {
|
result = set_remove(so, tmp);
|
||||||
Py_DECREF(tmp);
|
Py_DECREF(tmp);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (PyDict_DelItem(so->data, item) == -1)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
|
||||||
Py_DECREF(tmp);
|
|
||||||
}
|
|
||||||
Py_INCREF(Py_None);
|
Py_INCREF(Py_None);
|
||||||
return Py_None;
|
return Py_None;
|
||||||
}
|
}
|
||||||
|
@ -804,28 +802,21 @@ If the element is not a member, raise a KeyError.");
|
||||||
static PyObject *
|
static PyObject *
|
||||||
set_discard(PySetObject *so, PyObject *item)
|
set_discard(PySetObject *so, PyObject *item)
|
||||||
{
|
{
|
||||||
PyObject *tmp;
|
PyObject *tmp, *result;
|
||||||
|
|
||||||
if (PyDict_DelItem(so->data, item) == -1) {
|
if (PyType_IsSubtype(item->ob_type, &PySet_Type)) {
|
||||||
if (PyErr_ExceptionMatches(PyExc_KeyError))
|
|
||||||
PyErr_Clear();
|
|
||||||
else {
|
|
||||||
if (!PyType_IsSubtype(item->ob_type, &PySet_Type))
|
|
||||||
return NULL;
|
|
||||||
PyErr_Clear();
|
|
||||||
tmp = frozenset_dict_wrapper(((PySetObject *)(item))->data);
|
tmp = frozenset_dict_wrapper(((PySetObject *)(item))->data);
|
||||||
if (tmp == NULL)
|
if (tmp == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
if (PyDict_DelItem(so->data, tmp) == -1) {
|
result = set_discard(so, tmp);
|
||||||
if (PyErr_ExceptionMatches(PyExc_KeyError))
|
|
||||||
PyErr_Clear();
|
|
||||||
else {
|
|
||||||
Py_DECREF(tmp);
|
Py_DECREF(tmp);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (PyDict_DelItem(so->data, item) == -1) {
|
||||||
|
if (!PyErr_ExceptionMatches(PyExc_KeyError))
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
PyErr_Clear();
|
||||||
}
|
|
||||||
Py_DECREF(tmp);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Py_INCREF(Py_None);
|
Py_INCREF(Py_None);
|
||||||
return Py_None;
|
return Py_None;
|
||||||
|
@ -916,7 +907,7 @@ static PyMethodDef set_methods[] = {
|
||||||
add_doc},
|
add_doc},
|
||||||
{"clear", (PyCFunction)set_clear, METH_NOARGS,
|
{"clear", (PyCFunction)set_clear, METH_NOARGS,
|
||||||
clear_doc},
|
clear_doc},
|
||||||
{"__contains__", (PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
|
{"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
|
||||||
contains_doc},
|
contains_doc},
|
||||||
{"copy", (PyCFunction)set_copy, METH_NOARGS,
|
{"copy", (PyCFunction)set_copy, METH_NOARGS,
|
||||||
copy_doc},
|
copy_doc},
|
||||||
|
@ -1044,7 +1035,7 @@ PyTypeObject PySet_Type = {
|
||||||
|
|
||||||
|
|
||||||
static PyMethodDef frozenset_methods[] = {
|
static PyMethodDef frozenset_methods[] = {
|
||||||
{"__contains__", (PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
|
{"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
|
||||||
contains_doc},
|
contains_doc},
|
||||||
{"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
|
{"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
|
||||||
copy_doc},
|
copy_doc},
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue