From: Raymond Hettinger Date: Wed, 11 Jun 2008 10:30:54 +0000 (+0000) Subject: Multi-arg form for set.difference() and set.difference_update(). X-Git-Tag: v2.6b1~88 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=4267be6478d38445b8632e678e3cf459490bed6b;p=thirdparty%2FPython%2Fcpython.git Multi-arg form for set.difference() and set.difference_update(). --- diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index 758ee7bd6371..64d3c32f397c 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -1583,10 +1583,13 @@ The constructors for both classes work the same: .. versionchanged:: 2.6 Accepts multiple input iterables. - .. method:: difference(other) - set - other + .. method:: difference(other, ...) + set - other - ... - Return a new set with elements in the set that are not in *other*. + Return a new set with elements in the set that are not in the others. + + .. versionchanged:: 2.6 + Accepts multiple input iterables. .. method:: symmetric_difference(other) set ^ other @@ -1650,10 +1653,13 @@ The constructors for both classes work the same: .. versionchanged:: 2.6 Accepts multiple input iterables. - .. method:: difference_update(other) - set -= other + .. method:: difference_update(other, ...) + set -= other | ... - Update the set, removing elements found in *other*. + Update the set, removing elements found in others. + + .. versionchanged:: 2.6 + Accepts multiple input iterables. .. method:: symmetric_difference_update(other) set ^= other diff --git a/Lib/test/test_set.py b/Lib/test/test_set.py index b32d953a84fd..1b01954108e8 100644 --- a/Lib/test/test_set.py +++ b/Lib/test/test_set.py @@ -149,6 +149,8 @@ class TestJointOps(unittest.TestCase): self.assertEqual(self.thetype('abcba').difference(C('efgfe')), set('abc')) self.assertEqual(self.thetype('abcba').difference(C('ccb')), set('a')) self.assertEqual(self.thetype('abcba').difference(C('ef')), set('abc')) + self.assertEqual(self.thetype('abcba').difference(), set('abc')) + self.assertEqual(self.thetype('abcba').difference(C('a'), C('b')), set('c')) def test_sub(self): i = self.s.difference(self.otherword) @@ -467,6 +469,18 @@ class TestSet(TestJointOps): self.assertEqual(s.difference_update(C(p)), None) self.assertEqual(s, set(q)) + s = self.thetype('abcdefghih') + s.difference_update() + self.assertEqual(s, self.thetype('abcdefghih')) + + s = self.thetype('abcdefghih') + s.difference_update(C('aba')) + self.assertEqual(s, self.thetype('cdefghih')) + + s = self.thetype('abcdefghih') + s.difference_update(C('cdc'), C('aba')) + self.assertEqual(s, self.thetype('efghih')) + def test_isub(self): self.s -= set(self.otherword) for c in (self.word + self.otherword): diff --git a/Misc/NEWS b/Misc/NEWS index f721122363f4..afaf05b39c7a 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -13,7 +13,7 @@ Core and Builtins ----------------- - Several set methods now accept multiple arguments: update(), union(), - intersection() and intersection_update(). + intersection(), intersection_update(), difference(), and difference_update(). - Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes. diff --git a/Objects/setobject.c b/Objects/setobject.c index 1127680c071f..f3eea21745a7 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -1495,11 +1495,16 @@ set_difference_update_internal(PySetObject *so, PyObject *other) } static PyObject * -set_difference_update(PySetObject *so, PyObject *other) +set_difference_update(PySetObject *so, PyObject *args) { - if (set_difference_update_internal(so, other) != -1) - Py_RETURN_NONE; - return NULL; + Py_ssize_t i; + + for (i=0 ; i