Skip to content

Commit a8a930f

Browse files
committed
Issue python#25021: Merge 3.5 to default
2 parents 233cdb3 + d7f65e5 commit a8a930f

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
@@ -1035,6 +1035,16 @@ def test_product_pickling(self):
10351035
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
10361036
self.pickletest(proto, product(*args))
10371037

1038+
def test_product_issue_25021(self):
1039+
# test that indices are properly clamped to the length of the tuples
1040+
p = product((1, 2),(3,))
1041+
p.__setstate__((0, 0x1000)) # will access tuple element 1 if not clamped
1042+
self.assertEqual(next(p), (2, 3))
1043+
# test that empty tuple in the list will result in an immediate StopIteration
1044+
p = product((1, 2), (), (3,))
1045+
p.__setstate__((0, 0, 0x1000)) # will access tuple element 1 if not clamped
1046+
self.assertRaises(StopIteration, next, p)
1047+
10381048
def test_repeat(self):
10391049
self.assertEqual(list(repeat(object='a', times=3)), ['a', 'a', 'a'])
10401050
self.assertEqual(lzip(range(3),repeat('a')),

Modules/itertoolsmodule.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2256,13 +2256,21 @@ product_setstate(productobject *lz, PyObject *state)
22562256
{
22572257
PyObject* indexObject = PyTuple_GET_ITEM(state, i);
22582258
Py_ssize_t index = PyLong_AsSsize_t(indexObject);
2259+
PyObject* pool;
2260+
Py_ssize_t poolsize;
22592261
if (index < 0 && PyErr_Occurred())
22602262
return NULL; /* not an integer */
2263+
pool = PyTuple_GET_ITEM(lz->pools, i);
2264+
poolsize = PyTuple_GET_SIZE(pool);
2265+
if (poolsize == 0) {
2266+
lz->stopped = 1;
2267+
Py_RETURN_NONE;
2268+
}
22612269
/* clamp the index */
22622270
if (index < 0)
22632271
index = 0;
2264-
else if (index > n-1)
2265-
index = n-1;
2272+
else if (index > poolsize-1)
2273+
index = poolsize-1;
22662274
lz->indices[i] = index;
22672275
}
22682276

0 commit comments

Comments
 (0)