Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 95c3e6c

Browse files
committed
Issue #25021: Merge from 3.3 to 3.4
2 parents 0424eaf + 102764a commit 95c3e6c

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

Lib/test/test_itertools.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,16 @@ def test_product_pickling(self):
985985
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
986986
self.pickletest(proto, product(*args))
987987

988+
def test_product_issue_25021(self):
989+
# test that indices are properly clamped to the length of the tuples
990+
p = product((1, 2),(3,))
991+
p.__setstate__((0, 0x1000)) # will access tuple element 1 if not clamped
992+
self.assertEqual(next(p), (2, 3))
993+
# test that empty tuple in the list will result in an immediate StopIteration
994+
p = product((1, 2), (), (3,))
995+
p.__setstate__((0, 0, 0x1000)) # will access tuple element 1 if not clamped
996+
self.assertRaises(StopIteration, next, p)
997+
988998
def test_repeat(self):
989999
self.assertEqual(list(repeat(object='a', times=3)), ['a', 'a', 'a'])
9901000
self.assertEqual(lzip(range(3),repeat('a')),

Modules/itertoolsmodule.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2236,13 +2236,21 @@ product_setstate(productobject *lz, PyObject *state)
22362236
{
22372237
PyObject* indexObject = PyTuple_GET_ITEM(state, i);
22382238
Py_ssize_t index = PyLong_AsSsize_t(indexObject);
2239+
PyObject* pool;
2240+
Py_ssize_t poolsize;
22392241
if (index < 0 && PyErr_Occurred())
22402242
return NULL; /* not an integer */
2243+
pool = PyTuple_GET_ITEM(lz->pools, i);
2244+
poolsize = PyTuple_GET_SIZE(pool);
2245+
if (poolsize == 0) {
2246+
lz->stopped = 1;
2247+
Py_RETURN_NONE;
2248+
}
22412249
/* clamp the index */
22422250
if (index < 0)
22432251
index = 0;
2244-
else if (index > n-1)
2245-
index = n-1;
2252+
else if (index > poolsize-1)
2253+
index = poolsize-1;
22462254
lz->indices[i] = index;
22472255
}
22482256

0 commit comments

Comments
 (0)