summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2008-01-17 03:02:14 +0000
committerRaymond Hettinger <python@rcn.com>2008-01-17 03:02:14 +0000
commit473170908e11e347aca4adf23738a82162b981e3 (patch)
tree2fa25a5ccd4abfc71e6a0f5193838a726ce4c68a
parent3ad2acc8575e1977cece844b17c572550503a615 (diff)
downloadcpython-git-473170908e11e347aca4adf23738a82162b981e3.tar.gz
Make starmap() match its pure python definition and accept any itertable input (not just tuples).
-rw-r--r--Doc/library/itertools.rst10
-rw-r--r--Lib/test/test_itertools.py3
-rw-r--r--Misc/NEWS3
-rw-r--r--Modules/itertoolsmodule.c7
4 files changed, 15 insertions, 8 deletions
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst
index 788d931c28..e797aab6ac 100644
--- a/Doc/library/itertools.rst
+++ b/Doc/library/itertools.rst
@@ -330,17 +330,19 @@ loops that truncate the stream.
.. function:: starmap(function, iterable)
- Make an iterator that computes the function using arguments tuples obtained from
+ Make an iterator that computes the function using arguments obtained from
the iterable. Used instead of :func:`imap` when argument parameters are already
grouped in tuples from a single iterable (the data has been "pre-zipped"). The
difference between :func:`imap` and :func:`starmap` parallels the distinction
between ``function(a,b)`` and ``function(*c)``. Equivalent to::
def starmap(function, iterable):
- iterable = iter(iterable)
- while True:
- yield function(*iterable.next())
+ for args in iterable:
+ yield function(*args)
+ .. versionchanged:: 2.6
+ Previously, :func:`starmap` required the function arguments to be tuples.
+ Now, any iterable is allowed.
.. function:: takewhile(predicate, iterable)
diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py
index 1a3064c305..9d1922823d 100644
--- a/Lib/test/test_itertools.py
+++ b/Lib/test/test_itertools.py
@@ -292,7 +292,8 @@ class TestBasicOps(unittest.TestCase):
self.assertEqual(take(3, starmap(operator.pow, izip(count(), count(1)))),
[0**1, 1**2, 2**3])
self.assertEqual(list(starmap(operator.pow, [])), [])
- self.assertRaises(TypeError, list, starmap(operator.pow, [[4,5]]))
+ self.assertEqual(list(starmap(operator.pow, [iter([4,5])])), [4**5])
+ self.assertRaises(TypeError, list, starmap(operator.pow, [None]))
self.assertRaises(TypeError, starmap)
self.assertRaises(TypeError, starmap, operator.pow, [(4,5)], 'extra')
self.assertRaises(TypeError, starmap(10, [(4,5)]).next)
diff --git a/Misc/NEWS b/Misc/NEWS
index 9ec0f6ac9f..0085416963 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -993,6 +993,9 @@ Extension Modules
the context manager protocol. The _winreg module also gained a new function
``ExpandEnvironmentStrings`` to expand REG_EXPAND_SZ keys.
+- itertools.starmap() now accepts any iterable input. Previously, it required
+ the function inputs to be tuples.
+
- Issue #1646: Make socket support TIPC. The socket module now has support
for TIPC under Linux, see http://tipc.sf.net/ for more information.
diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c
index ebb4deb268..430313eaa6 100644
--- a/Modules/itertoolsmodule.c
+++ b/Modules/itertoolsmodule.c
@@ -1356,10 +1356,11 @@ starmap_next(starmapobject *lz)
if (args == NULL)
return NULL;
if (!PyTuple_CheckExact(args)) {
+ PyObject *newargs = PySequence_Tuple(args);
Py_DECREF(args);
- PyErr_SetString(PyExc_TypeError,
- "iterator must return a tuple");
- return NULL;
+ if (newargs == NULL)
+ return NULL;
+ args = newargs;
}
result = PyObject_Call(lz->func, args, NULL);
Py_DECREF(args);