| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -985,6 +985,16 @@ def test_product_pickling(self): | |||
| 985 | 985 | for proto in range(pickle.HIGHEST_PROTOCOL + 1): | |
| 986 | 986 | self.pickletest(proto, product(*args)) | |
| 987 | 987 | ||
| 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 | + | ||
| 988 | 998 | def test_repeat(self): | |
| 989 | 999 | self.assertEqual(list(repeat(object='a', times=3)), ['a', 'a', 'a']) | |
| 990 | 1000 | self.assertEqual(lzip(range(3),repeat('a')), | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2236,13 +2236,21 @@ product_setstate(productobject *lz, PyObject *state) | |||
| 2236 | 2236 | { | |
| 2237 | 2237 | PyObject* indexObject = PyTuple_GET_ITEM(state, i); | |
| 2238 | 2238 | Py_ssize_t index = PyLong_AsSsize_t(indexObject); | |
| 2239 | + PyObject* pool; | ||
| 2240 | + Py_ssize_t poolsize; | ||
| 2239 | 2241 | if (index < 0 && PyErr_Occurred()) | |
| 2240 | 2242 | 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 | + } | ||
| 2241 | 2249 | /* clamp the index */ | |
| 2242 | 2250 | if (index < 0) | |
| 2243 | 2251 | index = 0; | |
| 2244 | - else if (index > n-1) | ||
| 2245 | - index = n-1; | ||
| 2252 | + else if (index > poolsize-1) | ||
| 2253 | + index = poolsize-1; | ||
| 2246 | 2254 | lz->indices[i] = index; | |
| 2247 | 2255 | } | |
| 2248 | 2256 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments