Commit 3ec903fc authored by Antoine Pitrou's avatar Antoine Pitrou

Issue #21321: itertools.islice() now releases the reference to the source...

Issue #21321: itertools.islice() now releases the reference to the source iterator when the slice is exhausted.

Patch by Anton Afanasyev.
parent f3ae10e7
import unittest import unittest
from test import test_support from test import test_support
from itertools import * from itertools import *
from weakref import proxy import weakref
from decimal import Decimal from decimal import Decimal
from fractions import Fraction from fractions import Fraction
import sys import sys
...@@ -792,6 +792,15 @@ class TestBasicOps(unittest.TestCase): ...@@ -792,6 +792,15 @@ class TestBasicOps(unittest.TestCase):
self.assertEqual(list(islice(c, 1, 3, 50)), [1]) self.assertEqual(list(islice(c, 1, 3, 50)), [1])
self.assertEqual(next(c), 3) self.assertEqual(next(c), 3)
# Issue #21321: check source iterator is not referenced
# from islice() after the latter has been exhausted
it = (x for x in (1, 2))
wr = weakref.ref(it)
it = islice(it, 1)
self.assertIsNotNone(wr())
list(it) # exhaust the iterator
self.assertIsNone(wr())
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
...@@ -901,7 +910,7 @@ class TestBasicOps(unittest.TestCase): ...@@ -901,7 +910,7 @@ class TestBasicOps(unittest.TestCase):
# test that tee objects are weak referencable # test that tee objects are weak referencable
a, b = tee(xrange(10)) a, b = tee(xrange(10))
p = proxy(a) p = weakref.proxy(a)
self.assertEqual(getattr(p, '__class__'), type(b)) self.assertEqual(getattr(p, '__class__'), type(b))
del a del a
self.assertRaises(ReferenceError, getattr, p, '__class__') self.assertRaises(ReferenceError, getattr, p, '__class__')
......
...@@ -17,6 +17,7 @@ Rajiv Abraham ...@@ -17,6 +17,7 @@ Rajiv Abraham
David Abrahams David Abrahams
Marc Abramowitz Marc Abramowitz
Ron Adam Ron Adam
Anton Afanasyev
Ali Afshar Ali Afshar
Nitika Agarwal Nitika Agarwal
Jim Ahlstrom Jim Ahlstrom
......
...@@ -46,6 +46,9 @@ Core and Builtins ...@@ -46,6 +46,9 @@ Core and Builtins
Library Library
------- -------
- Issue #21321: itertools.islice() now releases the reference to the source
iterator when the slice is exhausted. Patch by Anton Afanasyev.
- Issue #9291: Do not attempt to re-encode mimetype data read from registry in - Issue #9291: Do not attempt to re-encode mimetype data read from registry in
ANSI mode. Initial patches by Dmitry Jemerov & Vladimir Iofik. ANSI mode. Initial patches by Dmitry Jemerov & Vladimir Iofik.
......
...@@ -1241,19 +1241,22 @@ islice_next(isliceobject *lz) ...@@ -1241,19 +1241,22 @@ islice_next(isliceobject *lz)
Py_ssize_t oldnext; Py_ssize_t oldnext;
PyObject *(*iternext)(PyObject *); PyObject *(*iternext)(PyObject *);
if (it == NULL)
return NULL;
iternext = *Py_TYPE(it)->tp_iternext; iternext = *Py_TYPE(it)->tp_iternext;
while (lz->cnt < lz->next) { while (lz->cnt < lz->next) {
item = iternext(it); item = iternext(it);
if (item == NULL) if (item == NULL)
return NULL; goto empty;
Py_DECREF(item); Py_DECREF(item);
lz->cnt++; lz->cnt++;
} }
if (stop != -1 && lz->cnt >= stop) if (stop != -1 && lz->cnt >= stop)
return NULL; goto empty;
item = iternext(it); item = iternext(it);
if (item == NULL) if (item == NULL)
return NULL; goto empty;
lz->cnt++; lz->cnt++;
oldnext = lz->next; oldnext = lz->next;
/* The (size_t) cast below avoids the danger of undefined /* The (size_t) cast below avoids the danger of undefined
...@@ -1262,6 +1265,10 @@ islice_next(isliceobject *lz) ...@@ -1262,6 +1265,10 @@ islice_next(isliceobject *lz)
if (lz->next < oldnext || (stop != -1 && lz->next > stop)) if (lz->next < oldnext || (stop != -1 && lz->next > stop))
lz->next = stop; lz->next = stop;
return item; return item;
empty:
Py_CLEAR(lz->it);
return NULL;
} }
PyDoc_STRVAR(islice_doc, PyDoc_STRVAR(islice_doc,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment