diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index c96ef10d7bb..1d8e053609d 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -1567,11 +1567,14 @@ The constructors for both classes work the same: Test whether the set is a true superset of *other*, that is, ``set >= other and set != other``. - .. method:: union(other) - set | other + .. method:: union(other, ...) + set | other | ... Return a new set with elements from both sets. + .. versionchanged:: 2.6 + Accepts multiple input iterables. + .. method:: intersection(other) set & other @@ -1628,11 +1631,14 @@ The constructors for both classes work the same: The following table lists operations available for :class:`set` that do not apply to immutable instances of :class:`frozenset`: - .. method:: update(other) - set |= other + .. method:: update(other, ...) + set |= other | ... Update the set, adding elements from *other*. + .. versionchanged:: 2.6 + Accepts multiple input iterables. + .. method:: intersection_update(other) set &= other diff --git a/Lib/test/test_set.py b/Lib/test/test_set.py index 6b3df3dd653..37a085cf77c 100644 --- a/Lib/test/test_set.py +++ b/Lib/test/test_set.py @@ -78,6 +78,7 @@ class TestJointOps(unittest.TestCase): self.assertEqual(self.thetype('abcba').union(C('efgfe')), set('abcefg')) self.assertEqual(self.thetype('abcba').union(C('ccb')), set('abc')) self.assertEqual(self.thetype('abcba').union(C('ef')), set('abcef')) + self.assertEqual(self.thetype('abcba').union(C('ef'), C('fg')), set('abcefg')) def test_or(self): i = self.s.union(self.otherword) @@ -401,6 +402,12 @@ class TestSet(TestJointOps): s = self.thetype('abcba') self.assertEqual(s.update(C(p)), None) self.assertEqual(s, set(q)) + for p in ('cdc', 'efgfe', 'ccb', 'ef', 'abcda'): + q = 'ahi' + for C in set, frozenset, dict.fromkeys, str, unicode, list, tuple: + s = self.thetype('abcba') + self.assertEqual(s.update(C(p), C(q)), None) + self.assertEqual(s, set(s) | set(p) | set(q)) def test_ior(self): self.s |= set(self.otherword) diff --git a/Misc/NEWS b/Misc/NEWS index 4337e5daff9..05f741975af 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -12,6 +12,8 @@ What's New in Python 2.6 beta 1? Core and Builtins ----------------- +- The set methods, update() and union() now accept multiple arguments. + - Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes. - New environment variable PYTHONIOENCODING. diff --git a/Objects/setobject.c b/Objects/setobject.c index 371d8c15417..908a9a3d097 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -967,15 +967,20 @@ set_update_internal(PySetObject *so, PyObject *other) } static PyObject * -set_update(PySetObject *so, PyObject *other) +set_update(PySetObject *so, PyObject *args) { - if (set_update_internal(so, other) == -1) - return NULL; + Py_ssize_t i; + + for (i=0 ; i