summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2007-08-24 23:41:22 (GMT)
committerGuido van Rossum <guido@python.org>2007-08-24 23:41:22 (GMT)
commit523259ba072e91605a8be25360ec402dae74bb2f (patch)
tree037a97f20df2071171a297c0090d50bc27705f59 /Objects
parenteb8b3a6d61860cdbf52af6e82b44925b81bb39b1 (diff)
downloadcpython-523259ba072e91605a8be25360ec402dae74bb2f.zip
cpython-523259ba072e91605a8be25360ec402dae74bb2f.tar.gz
cpython-523259ba072e91605a8be25360ec402dae74bb2f.tar.bz2
Keir Mierle's set operations for dict views (keys/items only of course).
Diffstat (limited to 'Objects')
-rw-r--r--Objects/dictobject.c96
1 files changed, 94 insertions, 2 deletions
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index 9ef1fcc..539d734 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -2489,6 +2489,98 @@ static PySequenceMethods dictkeys_as_sequence = {
(objobjproc)dictkeys_contains, /* sq_contains */
};
+static PyObject*
+dictviews_sub(PyObject* self, PyObject *other)
+{
+ PyObject *result = PySet_New(self);
+ PyObject *tmp;
+ if (result == NULL)
+ return NULL;
+
+ tmp = PyObject_CallMethod(result, "difference_update", "O", other);
+ if (tmp == NULL) {
+ Py_DECREF(result);
+ return NULL;
+ }
+
+ Py_DECREF(tmp);
+ return result;
+}
+
+static PyObject*
+dictviews_and(PyObject* self, PyObject *other)
+{
+ PyObject *result = PySet_New(self);
+ PyObject *tmp;
+ if (result == NULL)
+ return NULL;
+
+ tmp = PyObject_CallMethod(result, "intersection_update", "O", other);
+ if (tmp == NULL) {
+ Py_DECREF(result);
+ return NULL;
+ }
+
+ Py_DECREF(tmp);
+ return result;
+}
+
+static PyObject*
+dictviews_or(PyObject* self, PyObject *other)
+{
+ PyObject *result = PySet_New(self);
+ PyObject *tmp;
+ if (result == NULL)
+ return NULL;
+
+ tmp = PyObject_CallMethod(result, "update", "O", other);
+ if (tmp == NULL) {
+ Py_DECREF(result);
+ return NULL;
+ }
+
+ Py_DECREF(tmp);
+ return result;
+}
+
+static PyObject*
+dictviews_xor(PyObject* self, PyObject *other)
+{
+ PyObject *result = PySet_New(self);
+ PyObject *tmp;
+ if (result == NULL)
+ return NULL;
+
+ tmp = PyObject_CallMethod(result, "symmetric_difference_update", "O",
+ other);
+ if (tmp == NULL) {
+ Py_DECREF(result);
+ return NULL;
+ }
+
+ Py_DECREF(tmp);
+ return result;
+}
+
+static PyNumberMethods dictviews_as_number = {
+ 0, /*nb_add*/
+ (binaryfunc)dictviews_sub, /*nb_subtract*/
+ 0, /*nb_multiply*/
+ 0, /*nb_remainder*/
+ 0, /*nb_divmod*/
+ 0, /*nb_power*/
+ 0, /*nb_negative*/
+ 0, /*nb_positive*/
+ 0, /*nb_absolute*/
+ 0, /*nb_bool*/
+ 0, /*nb_invert*/
+ 0, /*nb_lshift*/
+ 0, /*nb_rshift*/
+ (binaryfunc)dictviews_and, /*nb_and*/
+ (binaryfunc)dictviews_xor, /*nb_xor*/
+ (binaryfunc)dictviews_or, /*nb_or*/
+};
+
static PyMethodDef dictkeys_methods[] = {
{NULL, NULL} /* sentinel */
};
@@ -2505,7 +2597,7 @@ PyTypeObject PyDictKeys_Type = {
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
- 0, /* tp_as_number */
+ &dictviews_as_number, /* tp_as_number */
&dictkeys_as_sequence, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
@@ -2589,7 +2681,7 @@ PyTypeObject PyDictItems_Type = {
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
- 0, /* tp_as_number */
+ &dictviews_as_number, /* tp_as_number */
&dictitems_as_sequence, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */