[Python-checkins] r64055 - in python/trunk: Doc/library/stdtypes.rst Lib/test/test_set.py Misc/NEWS Objects/setobject.c

raymond.hettinger python-checkins at python.org
Mon Jun 9 15:07:28 CEST 2008


Author: raymond.hettinger
Date: Mon Jun  9 15:07:27 2008
New Revision: 64055

Log:
Let set.intersection() and set.intersection_update() take multiple input arguments.

Modified:
   python/trunk/Doc/library/stdtypes.rst
   python/trunk/Lib/test/test_set.py
   python/trunk/Misc/NEWS
   python/trunk/Objects/setobject.c

Modified: python/trunk/Doc/library/stdtypes.rst
==============================================================================
--- python/trunk/Doc/library/stdtypes.rst	(original)
+++ python/trunk/Doc/library/stdtypes.rst	Mon Jun  9 15:07:27 2008
@@ -1575,11 +1575,14 @@
       .. versionchanged:: 2.6
          Accepts multiple input iterables.
 
-   .. method:: intersection(other)
-               set & other
+   .. method:: intersection(other, ...)
+               set & other & ...
 
       Return a new set with elements common to both sets.
 
+      .. versionchanged:: 2.6
+         Accepts multiple input iterables.
+
    .. method:: difference(other)
                set - other
 
@@ -1639,11 +1642,14 @@
       .. versionchanged:: 2.6
          Accepts multiple input iterables.
 
-   .. method:: intersection_update(other)
-               set &= other
+   .. method:: intersection_update(other, ...)
+               set &= other & ...
 
       Update the set, keeping only elements found in it and *other*.
 
+      .. versionchanged:: 2.6
+         Accepts multiple input iterables.
+
    .. method:: difference_update(other)
                set -= other
 

Modified: python/trunk/Lib/test/test_set.py
==============================================================================
--- python/trunk/Lib/test/test_set.py	(original)
+++ python/trunk/Lib/test/test_set.py	Mon Jun  9 15:07:27 2008
@@ -103,6 +103,7 @@
             self.assertEqual(self.thetype('abcba').intersection(C('efgfe')), set(''))
             self.assertEqual(self.thetype('abcba').intersection(C('ccb')), set('bc'))
             self.assertEqual(self.thetype('abcba').intersection(C('ef')), set(''))
+            self.assertEqual(self.thetype('abcba').intersection(C('cbcf'), C('bag')), set('b'))
 
     def test_isdisjoint(self):
         def f(s1, s2):
@@ -429,6 +430,11 @@
                 s = self.thetype('abcba')
                 self.assertEqual(s.intersection_update(C(p)), None)
                 self.assertEqual(s, set(q))
+                ss = 'abcba'
+                s = self.thetype(ss)
+                t = 'cbc'
+                self.assertEqual(s.intersection_update(C(p), C(t)), None)
+                self.assertEqual(s, set('abcba')&set(p)&set(t))
 
     def test_iand(self):
         self.s &= set(self.otherword)

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Mon Jun  9 15:07:27 2008
@@ -12,7 +12,8 @@
 Core and Builtins
 -----------------
 
-- The set methods, update() and union() now accept multiple arguments.
+- Several set methods now accept multiple arguments:  update(), union(),
+  intersection() and intersection_update().
 
 - Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
 

Modified: python/trunk/Objects/setobject.c
==============================================================================
--- python/trunk/Objects/setobject.c	(original)
+++ python/trunk/Objects/setobject.c	Mon Jun  9 15:07:27 2008
@@ -1306,6 +1306,26 @@
 	return (PyObject *)result;
 }
 
+static PyObject *
+set_intersection_multi(PySetObject *so, PyObject *args)
+{
+	Py_ssize_t i;
+	PyObject *result = (PyObject *)so;
+
+	Py_INCREF(so);
+	for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
+		PyObject *other = PyTuple_GET_ITEM(args, i);
+		PyObject *newresult = set_intersection((PySetObject *)result, other);
+		if (newresult == NULL) {
+			Py_DECREF(result);
+			return NULL;
+		}
+		Py_DECREF(result);
+		result = newresult;
+	}
+	return result;
+}
+
 PyDoc_STRVAR(intersection_doc,
 "Return the intersection of two sets as a new set.\n\
 \n\
@@ -1324,6 +1344,19 @@
 	Py_RETURN_NONE;
 }
 
+static PyObject *
+set_intersection_update_multi(PySetObject *so, PyObject *args)
+{
+	PyObject *tmp;
+
+	tmp = set_intersection_multi(so, args);
+	if (tmp == NULL)
+		return NULL;
+	set_swap_bodies(so, (PySetObject *)tmp);
+	Py_DECREF(tmp);
+	Py_RETURN_NONE;
+}
+
 PyDoc_STRVAR(intersection_update_doc,
 "Update a set with the intersection of itself and another.");
 
@@ -1946,9 +1979,9 @@
 	 difference_doc},
 	{"difference_update",	(PyCFunction)set_difference_update,	METH_O,
 	 difference_update_doc},
-	{"intersection",(PyCFunction)set_intersection,	METH_O,
+	{"intersection",(PyCFunction)set_intersection_multi,	METH_VARARGS,
 	 intersection_doc},
-	{"intersection_update",(PyCFunction)set_intersection_update,	METH_O,
+	{"intersection_update",(PyCFunction)set_intersection_update_multi,	METH_VARARGS,
 	 intersection_update_doc},
 	{"isdisjoint",	(PyCFunction)set_isdisjoint,	METH_O,
 	 isdisjoint_doc},
@@ -2073,7 +2106,7 @@
 	 copy_doc},
 	{"difference",	(PyCFunction)set_difference,	METH_O,
 	 difference_doc},
-	{"intersection",(PyCFunction)set_intersection,	METH_O,
+	{"intersection",(PyCFunction)set_intersection_multi,	METH_VARARGS,
 	 intersection_doc},
 	{"isdisjoint",	(PyCFunction)set_isdisjoint,	METH_O,
 	 isdisjoint_doc},


More information about the Python-checkins mailing list