Factor out more duplicate code.
This commit is contained in:
parent
7e0282f1b1
commit
a38123e2fa
@ -10,46 +10,67 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
make_new_set(PyTypeObject *type, PyObject *iterable)
|
set_update(PySetObject *so, PyObject *other)
|
||||||
{
|
{
|
||||||
PyObject *data = NULL;
|
PyObject *item, *data, *it;
|
||||||
PyObject *it = NULL;
|
|
||||||
PyObject *item;
|
|
||||||
PySetObject *so = NULL;
|
|
||||||
|
|
||||||
/* Get iterator. */
|
if (PyAnySet_Check(other)) {
|
||||||
if (iterable != NULL) {
|
if (PyDict_Merge(so->data, ((PySetObject *)other)->data, 0) == -1)
|
||||||
it = PyObject_GetIter(iterable);
|
return NULL;
|
||||||
if (it == NULL)
|
Py_RETURN_NONE;
|
||||||
goto done;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
data = PyDict_New();
|
it = PyObject_GetIter(other);
|
||||||
if (data == NULL)
|
if (it == NULL)
|
||||||
goto done;
|
return NULL;
|
||||||
|
data = so->data;
|
||||||
|
|
||||||
while (it != NULL && (item = PyIter_Next(it)) != NULL) {
|
while ((item = PyIter_Next(it)) != NULL) {
|
||||||
if (PyDict_SetItem(data, item, Py_True) == -1) {
|
if (PyDict_SetItem(data, item, Py_True) == -1) {
|
||||||
|
Py_DECREF(it);
|
||||||
Py_DECREF(item);
|
Py_DECREF(item);
|
||||||
goto done;
|
return NULL;
|
||||||
}
|
}
|
||||||
Py_DECREF(item);
|
Py_DECREF(item);
|
||||||
}
|
}
|
||||||
|
Py_DECREF(it);
|
||||||
if (PyErr_Occurred())
|
if (PyErr_Occurred())
|
||||||
goto done;
|
return NULL;
|
||||||
|
Py_RETURN_NONE;
|
||||||
|
}
|
||||||
|
|
||||||
|
PyDoc_STRVAR(update_doc,
|
||||||
|
"Update a set with the union of itself and another.");
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
make_new_set(PyTypeObject *type, PyObject *iterable)
|
||||||
|
{
|
||||||
|
PyObject *data = NULL;
|
||||||
|
PyObject *tmp;
|
||||||
|
PySetObject *so = NULL;
|
||||||
|
|
||||||
|
data = PyDict_New();
|
||||||
|
if (data == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
/* create PySetObject structure */
|
/* create PySetObject structure */
|
||||||
so = (PySetObject *)type->tp_alloc(type, 0);
|
so = (PySetObject *)type->tp_alloc(type, 0);
|
||||||
if (so == NULL)
|
if (so == NULL) {
|
||||||
goto done;
|
Py_DECREF(data);
|
||||||
|
return NULL;
|
||||||
Py_INCREF(data);
|
}
|
||||||
so->data = data;
|
so->data = data;
|
||||||
so->hash = -1;
|
so->hash = -1;
|
||||||
|
|
||||||
done:
|
if (iterable != NULL) {
|
||||||
Py_XDECREF(data);
|
tmp = set_update(so, iterable);
|
||||||
Py_XDECREF(it);
|
if (tmp == NULL) {
|
||||||
|
Py_DECREF(so);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
Py_DECREF(tmp);
|
||||||
|
}
|
||||||
|
|
||||||
return (PyObject *)so;
|
return (PyObject *)so;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,7 +144,7 @@ set_contains(PySetObject *so, PyObject *key)
|
|||||||
int result;
|
int result;
|
||||||
|
|
||||||
result = PySequence_Contains(so->data, key);
|
result = PySequence_Contains(so->data, key);
|
||||||
if (result == -1 && PyType_IsSubtype(key->ob_type, &PySet_Type)) {
|
if (result == -1 && PyAnySet_Check(key)) {
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
tmp = frozenset_dict_wrapper(((PySetObject *)(key))->data);
|
tmp = frozenset_dict_wrapper(((PySetObject *)(key))->data);
|
||||||
if (tmp == NULL)
|
if (tmp == NULL)
|
||||||
@ -137,21 +158,7 @@ set_contains(PySetObject *so, PyObject *key)
|
|||||||
static PyObject *
|
static PyObject *
|
||||||
set_copy(PySetObject *so)
|
set_copy(PySetObject *so)
|
||||||
{
|
{
|
||||||
PyObject *data;
|
return make_new_set(so->ob_type, (PyObject *)so);
|
||||||
PySetObject *newso;
|
|
||||||
|
|
||||||
data = PyDict_Copy(so->data);
|
|
||||||
if (data == NULL)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
newso = (PySetObject *)(so->ob_type->tp_alloc(so->ob_type, 0));
|
|
||||||
if (newso == NULL) {
|
|
||||||
Py_DECREF(data);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
newso->data = data;
|
|
||||||
newso->hash = so->hash;
|
|
||||||
return (PyObject *)newso;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
@ -166,39 +173,6 @@ frozenset_copy(PySetObject *so)
|
|||||||
|
|
||||||
PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
|
PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
|
||||||
|
|
||||||
static PyObject *
|
|
||||||
set_union_update(PySetObject *so, PyObject *other)
|
|
||||||
{
|
|
||||||
PyObject *item, *data, *it;
|
|
||||||
|
|
||||||
if (PyAnySet_Check(other)) {
|
|
||||||
if (PyDict_Merge(so->data, ((PySetObject *)other)->data, 0) == -1)
|
|
||||||
return NULL;
|
|
||||||
Py_RETURN_NONE;
|
|
||||||
}
|
|
||||||
|
|
||||||
it = PyObject_GetIter(other);
|
|
||||||
if (it == NULL)
|
|
||||||
return NULL;
|
|
||||||
data = so->data;
|
|
||||||
|
|
||||||
while ((item = PyIter_Next(it)) != NULL) {
|
|
||||||
if (PyDict_SetItem(data, item, Py_True) == -1) {
|
|
||||||
Py_DECREF(it);
|
|
||||||
Py_DECREF(item);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
Py_DECREF(item);
|
|
||||||
}
|
|
||||||
Py_DECREF(it);
|
|
||||||
if (PyErr_Occurred())
|
|
||||||
return NULL;
|
|
||||||
Py_RETURN_NONE;
|
|
||||||
}
|
|
||||||
|
|
||||||
PyDoc_STRVAR(union_update_doc,
|
|
||||||
"Update a set with the union of itself and another.");
|
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
set_union(PySetObject *so, PyObject *other)
|
set_union(PySetObject *so, PyObject *other)
|
||||||
{
|
{
|
||||||
@ -208,7 +182,7 @@ set_union(PySetObject *so, PyObject *other)
|
|||||||
result = (PySetObject *)set_copy(so);
|
result = (PySetObject *)set_copy(so);
|
||||||
if (result == NULL)
|
if (result == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
rv = set_union_update(result, other);
|
rv = set_update(result, other);
|
||||||
if (rv == NULL) {
|
if (rv == NULL) {
|
||||||
Py_DECREF(result);
|
Py_DECREF(result);
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -241,7 +215,7 @@ set_ior(PySetObject *so, PyObject *other)
|
|||||||
Py_INCREF(Py_NotImplemented);
|
Py_INCREF(Py_NotImplemented);
|
||||||
return Py_NotImplemented;
|
return Py_NotImplemented;
|
||||||
}
|
}
|
||||||
result = set_union_update(so, other);
|
result = set_update(so, other);
|
||||||
if (result == NULL)
|
if (result == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
Py_DECREF(result);
|
Py_DECREF(result);
|
||||||
@ -968,7 +942,7 @@ set_init(PySetObject *self, PyObject *args, PyObject *kwds)
|
|||||||
self->hash = -1;
|
self->hash = -1;
|
||||||
if (iterable == NULL)
|
if (iterable == NULL)
|
||||||
return 0;
|
return 0;
|
||||||
result = set_union_update(self, iterable);
|
result = set_update(self, iterable);
|
||||||
if (result != NULL) {
|
if (result != NULL) {
|
||||||
Py_DECREF(result);
|
Py_DECREF(result);
|
||||||
return 0;
|
return 0;
|
||||||
@ -1024,8 +998,8 @@ static PyMethodDef set_methods[] = {
|
|||||||
symmetric_difference_update_doc},
|
symmetric_difference_update_doc},
|
||||||
{"union", (PyCFunction)set_union, METH_O,
|
{"union", (PyCFunction)set_union, METH_O,
|
||||||
union_doc},
|
union_doc},
|
||||||
{"update", (PyCFunction)set_union_update, METH_O,
|
{"update", (PyCFunction)set_update, METH_O,
|
||||||
union_update_doc},
|
update_doc},
|
||||||
{NULL, NULL} /* sentinel */
|
{NULL, NULL} /* sentinel */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user