summaryrefslogtreecommitdiffstats
path: root/Objects/setobject.c
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2003-12-13 18:53:18 (GMT)
committerRaymond Hettinger <python@rcn.com>2003-12-13 18:53:18 (GMT)
commit0deab62704d56d9c30933998dee7b6d9ca7b6c72 (patch)
treec94318e6e3f51757c8d3049b7d39564c45afa369 /Objects/setobject.c
parent6a8bbdbe7b97cbcafdf32db9d733441a5ff20683 (diff)
downloadcpython-0deab62704d56d9c30933998dee7b6d9ca7b6c72.zip
cpython-0deab62704d56d9c30933998dee7b6d9ca7b6c72.tar.gz
cpython-0deab62704d56d9c30933998dee7b6d9ca7b6c72.tar.bz2
Refactor set.discard() and set.remove().
Diffstat (limited to 'Objects/setobject.c')
-rw-r--r--Objects/setobject.c53
1 files changed, 22 insertions, 31 deletions
diff --git a/Objects/setobject.c b/Objects/setobject.c
index cc8d25c..82ff683 100644
--- a/Objects/setobject.c
+++ b/Objects/setobject.c
@@ -777,21 +777,19 @@ This has no effect if the element is already present.");
static PyObject *
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))
- return NULL;
- PyErr_Clear();
+ if (PyType_IsSubtype(item->ob_type, &PySet_Type)) {
tmp = frozenset_dict_wrapper(((PySetObject *)(item))->data);
if (tmp == NULL)
return NULL;
- if (PyDict_DelItem(so->data, tmp) == -1) {
- Py_DECREF(tmp);
- return NULL;
- }
+ result = set_remove(so, tmp);
Py_DECREF(tmp);
+ return result;
}
+
+ if (PyDict_DelItem(so->data, item) == -1)
+ return NULL;
Py_INCREF(Py_None);
return Py_None;
}
@@ -804,28 +802,21 @@ If the element is not a member, raise a KeyError.");
static PyObject *
set_discard(PySetObject *so, PyObject *item)
{
- PyObject *tmp;
+ PyObject *tmp, *result;
+
+ if (PyType_IsSubtype(item->ob_type, &PySet_Type)) {
+ tmp = frozenset_dict_wrapper(((PySetObject *)(item))->data);
+ if (tmp == NULL)
+ return NULL;
+ result = set_discard(so, tmp);
+ Py_DECREF(tmp);
+ return result;
+ }
if (PyDict_DelItem(so->data, item) == -1) {
- 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);
- if (tmp == NULL)
- return NULL;
- if (PyDict_DelItem(so->data, tmp) == -1) {
- if (PyErr_ExceptionMatches(PyExc_KeyError))
- PyErr_Clear();
- else {
- Py_DECREF(tmp);
- return NULL;
- }
- }
- Py_DECREF(tmp);
- }
+ if (!PyErr_ExceptionMatches(PyExc_KeyError))
+ return NULL;
+ PyErr_Clear();
}
Py_INCREF(Py_None);
return Py_None;
@@ -916,7 +907,7 @@ static PyMethodDef set_methods[] = {
add_doc},
{"clear", (PyCFunction)set_clear, METH_NOARGS,
clear_doc},
- {"__contains__", (PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
+ {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
contains_doc},
{"copy", (PyCFunction)set_copy, METH_NOARGS,
copy_doc},
@@ -1044,7 +1035,7 @@ PyTypeObject PySet_Type = {
static PyMethodDef frozenset_methods[] = {
- {"__contains__", (PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
+ {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
contains_doc},
{"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
copy_doc},