mirror of
https://github.com/python/cpython.git
synced 2025-11-21 11:19:43 +00:00
Make starmap() match its pure python definition and accept any itertable input (not just tuples).
This commit is contained in:
parent
3ad2acc857
commit
473170908e
4 changed files with 15 additions and 8 deletions
|
|
@ -330,17 +330,19 @@ loops that truncate the stream.
|
||||||
|
|
||||||
.. function:: starmap(function, iterable)
|
.. 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
|
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
|
grouped in tuples from a single iterable (the data has been "pre-zipped"). The
|
||||||
difference between :func:`imap` and :func:`starmap` parallels the distinction
|
difference between :func:`imap` and :func:`starmap` parallels the distinction
|
||||||
between ``function(a,b)`` and ``function(*c)``. Equivalent to::
|
between ``function(a,b)`` and ``function(*c)``. Equivalent to::
|
||||||
|
|
||||||
def starmap(function, iterable):
|
def starmap(function, iterable):
|
||||||
iterable = iter(iterable)
|
for args in iterable:
|
||||||
while True:
|
yield function(*args)
|
||||||
yield function(*iterable.next())
|
|
||||||
|
|
||||||
|
.. versionchanged:: 2.6
|
||||||
|
Previously, :func:`starmap` required the function arguments to be tuples.
|
||||||
|
Now, any iterable is allowed.
|
||||||
|
|
||||||
.. function:: takewhile(predicate, iterable)
|
.. function:: takewhile(predicate, iterable)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -292,7 +292,8 @@ class TestBasicOps(unittest.TestCase):
|
||||||
self.assertEqual(take(3, starmap(operator.pow, izip(count(), count(1)))),
|
self.assertEqual(take(3, starmap(operator.pow, izip(count(), count(1)))),
|
||||||
[0**1, 1**2, 2**3])
|
[0**1, 1**2, 2**3])
|
||||||
self.assertEqual(list(starmap(operator.pow, [])), [])
|
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)
|
||||||
self.assertRaises(TypeError, starmap, operator.pow, [(4,5)], 'extra')
|
self.assertRaises(TypeError, starmap, operator.pow, [(4,5)], 'extra')
|
||||||
self.assertRaises(TypeError, starmap(10, [(4,5)]).next)
|
self.assertRaises(TypeError, starmap(10, [(4,5)]).next)
|
||||||
|
|
|
||||||
|
|
@ -993,6 +993,9 @@ Extension Modules
|
||||||
the context manager protocol. The _winreg module also gained a new function
|
the context manager protocol. The _winreg module also gained a new function
|
||||||
``ExpandEnvironmentStrings`` to expand REG_EXPAND_SZ keys.
|
``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
|
- Issue #1646: Make socket support TIPC. The socket module now has support
|
||||||
for TIPC under Linux, see http://tipc.sf.net/ for more information.
|
for TIPC under Linux, see http://tipc.sf.net/ for more information.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1356,10 +1356,11 @@ starmap_next(starmapobject *lz)
|
||||||
if (args == NULL)
|
if (args == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
if (!PyTuple_CheckExact(args)) {
|
if (!PyTuple_CheckExact(args)) {
|
||||||
|
PyObject *newargs = PySequence_Tuple(args);
|
||||||
Py_DECREF(args);
|
Py_DECREF(args);
|
||||||
PyErr_SetString(PyExc_TypeError,
|
if (newargs == NULL)
|
||||||
"iterator must return a tuple");
|
|
||||||
return NULL;
|
return NULL;
|
||||||
|
args = newargs;
|
||||||
}
|
}
|
||||||
result = PyObject_Call(lz->func, args, NULL);
|
result = PyObject_Call(lz->func, args, NULL);
|
||||||
Py_DECREF(args);
|
Py_DECREF(args);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue