mirror of
https://github.com/python/cpython.git
synced 2025-08-17 15:21:26 +00:00
Issue #10323: Predictable final state for slice().
This commit is contained in:
parent
1fea5c4472
commit
061bf7a11a
3 changed files with 13 additions and 3 deletions
|
@ -778,6 +778,11 @@ class TestBasicOps(unittest.TestCase):
|
||||||
self.assertRaises(ValueError, islice, xrange(10), 1, 'a', 1)
|
self.assertRaises(ValueError, islice, xrange(10), 1, 'a', 1)
|
||||||
self.assertEqual(len(list(islice(count(), 1, 10, maxsize))), 1)
|
self.assertEqual(len(list(islice(count(), 1, 10, maxsize))), 1)
|
||||||
|
|
||||||
|
# Issue #10323: Less islice in a predictable state
|
||||||
|
c = count()
|
||||||
|
self.assertEqual(list(islice(c, 1, 3, 50)), [1])
|
||||||
|
self.assertEqual(next(c), 3)
|
||||||
|
|
||||||
def test_takewhile(self):
|
def test_takewhile(self):
|
||||||
data = [1, 3, 5, 20, 2, 4, 6, 8]
|
data = [1, 3, 5, 20, 2, 4, 6, 8]
|
||||||
underten = lambda x: x<10
|
underten = lambda x: x<10
|
||||||
|
|
|
@ -22,6 +22,10 @@ What's New in Python 2.7.1?
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #10323: itertools.islice() now consumes the minimum number of
|
||||||
|
inputs before stopping. Formerly, the final state of the underlying
|
||||||
|
iterator was undefined.
|
||||||
|
|
||||||
- Issue #10565: The collections.Iterator ABC now checks for both
|
- Issue #10565: The collections.Iterator ABC now checks for both
|
||||||
``__iter__`` and ``next``.
|
``__iter__`` and ``next``.
|
||||||
|
|
||||||
|
|
|
@ -1215,6 +1215,7 @@ islice_next(isliceobject *lz)
|
||||||
{
|
{
|
||||||
PyObject *item;
|
PyObject *item;
|
||||||
PyObject *it = lz->it;
|
PyObject *it = lz->it;
|
||||||
|
Py_ssize_t stop = lz->stop;
|
||||||
Py_ssize_t oldnext;
|
Py_ssize_t oldnext;
|
||||||
PyObject *(*iternext)(PyObject *);
|
PyObject *(*iternext)(PyObject *);
|
||||||
|
|
||||||
|
@ -1226,7 +1227,7 @@ islice_next(isliceobject *lz)
|
||||||
Py_DECREF(item);
|
Py_DECREF(item);
|
||||||
lz->cnt++;
|
lz->cnt++;
|
||||||
}
|
}
|
||||||
if (lz->stop != -1 && lz->cnt >= lz->stop)
|
if (stop != -1 && lz->cnt >= stop)
|
||||||
return NULL;
|
return NULL;
|
||||||
item = iternext(it);
|
item = iternext(it);
|
||||||
if (item == NULL)
|
if (item == NULL)
|
||||||
|
@ -1234,8 +1235,8 @@ islice_next(isliceobject *lz)
|
||||||
lz->cnt++;
|
lz->cnt++;
|
||||||
oldnext = lz->next;
|
oldnext = lz->next;
|
||||||
lz->next += lz->step;
|
lz->next += lz->step;
|
||||||
if (lz->next < oldnext) /* Check for overflow */
|
if (lz->next < oldnext || (stop != -1 && lz->next > stop))
|
||||||
lz->next = lz->stop;
|
lz->next = stop;
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue