diff -r 8c8315bac6a8 Lib/test/test_itertools.py --- a/Lib/test/test_itertools.py Sun Apr 20 09:45:00 2014 -0700 +++ b/Lib/test/test_itertools.py Mon Apr 28 22:54:27 2014 +0400 @@ -1087,6 +1087,15 @@ list(range(*args))) self.pickletest(islice(range(100), *args)) + # Issue #21321: check source iterator is not referenced + # from islice() after the latter has been exhausted + a = [random.random() for i in range(10)] + before = sys.getrefcount(a) + b = islice(a, 5) + for i in b: pass + after = sys.getrefcount(a) + self.assertEqual(before, after) + def test_takewhile(self): data = [1, 3, 5, 20, 2, 4, 6, 8] self.assertEqual(list(takewhile(underten, data)), [1, 3, 5]) diff -r 8c8315bac6a8 Modules/itertoolsmodule.c --- a/Modules/itertoolsmodule.c Sun Apr 20 09:45:00 2014 -0700 +++ b/Modules/itertoolsmodule.c Mon Apr 28 22:54:27 2014 +0400 @@ -1492,19 +1492,22 @@ Py_ssize_t oldnext; PyObject *(*iternext)(PyObject *); + if (it == NULL) + return NULL; + iternext = *Py_TYPE(it)->tp_iternext; while (lz->cnt < lz->next) { item = iternext(it); if (item == NULL) - return NULL; + goto empty; Py_DECREF(item); lz->cnt++; } if (stop != -1 && lz->cnt >= stop) - return NULL; + goto empty; item = iternext(it); if (item == NULL) - return NULL; + goto empty; lz->cnt++; oldnext = lz->next; /* The (size_t) cast below avoids the danger of undefined @@ -1513,6 +1516,10 @@ if (lz->next < oldnext || (stop != -1 && lz->next > stop)) lz->next = stop; return item; + +empty: + Py_CLEAR(lz->it); + return NULL; } static PyObject * @@ -1522,6 +1529,19 @@ * then 'setstate' with the next and count */ PyObject *stop; + if (lz->it == NULL) { + PyObject *empty_list; + PyObject *empty_it; + empty_list = PyList_New(0); + if (empty_list == NULL) + return NULL; + empty_it = PyObject_GetIter(empty_list); + if (empty_it == NULL) { + Py_DECREF(empty_list); + return NULL; + } + return Py_BuildValue("O(On)n", Py_TYPE(lz), empty_it, 0, 0); + } if (lz->stop == -1) { stop = Py_None; Py_INCREF(stop);