mirror of
https://github.com/python/cpython.git
synced 2025-08-10 03:49:18 +00:00
bpo-45018: Fix rangeiter_reduce in rangeobject.c (GH-27938) (GH-27991)
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
(cherry picked from commit 94a3d2a632
)
Co-authored-by: chilaxan <chilaxan@gmail.com>
This commit is contained in:
parent
f1e3fc4631
commit
02437641d2
3 changed files with 32 additions and 16 deletions
|
@ -374,26 +374,41 @@ class RangeTest(unittest.TestCase):
|
|||
list(r))
|
||||
|
||||
def test_iterator_pickling(self):
|
||||
testcases = [(13,), (0, 11), (-22, 10), (20, 3, -1),
|
||||
(13, 21, 3), (-2, 2, 2), (2**65, 2**65+2)]
|
||||
testcases = [(13,), (0, 11), (-22, 10), (20, 3, -1), (13, 21, 3),
|
||||
(-2, 2, 2), (2**31-3, 2**31-1), (2**33, 2**33+2),
|
||||
(2**63-3, 2**63-1), (2**65, 2**65+2)]
|
||||
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||
for t in testcases:
|
||||
it = itorg = iter(range(*t))
|
||||
data = list(range(*t))
|
||||
with self.subTest(proto=proto, t=t):
|
||||
it = itorg = iter(range(*t))
|
||||
data = list(range(*t))
|
||||
|
||||
d = pickle.dumps(it, proto)
|
||||
it = pickle.loads(d)
|
||||
self.assertEqual(type(itorg), type(it))
|
||||
self.assertEqual(list(it), data)
|
||||
|
||||
it = pickle.loads(d)
|
||||
try:
|
||||
next(it)
|
||||
except StopIteration:
|
||||
continue
|
||||
d = pickle.dumps(it, proto)
|
||||
it = pickle.loads(d)
|
||||
self.assertEqual(list(it), data[1:])
|
||||
|
||||
def test_iterator_pickling_overflowing_index(self):
|
||||
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||
with self.subTest(proto=proto):
|
||||
it = iter(range(2**32 + 2))
|
||||
_, _, idx = it.__reduce__()
|
||||
self.assertEqual(idx, 0)
|
||||
it.__setstate__(2**32 + 1) # undocumented way to set r->index
|
||||
_, _, idx = it.__reduce__()
|
||||
self.assertEqual(idx, 2**32 + 1)
|
||||
d = pickle.dumps(it, proto)
|
||||
it = pickle.loads(d)
|
||||
self.assertEqual(type(itorg), type(it))
|
||||
self.assertEqual(list(it), data)
|
||||
|
||||
it = pickle.loads(d)
|
||||
try:
|
||||
next(it)
|
||||
except StopIteration:
|
||||
continue
|
||||
d = pickle.dumps(it, proto)
|
||||
it = pickle.loads(d)
|
||||
self.assertEqual(list(it), data[1:])
|
||||
self.assertEqual(next(it), 2**32 + 1)
|
||||
|
||||
def test_exhausted_iterator_pickling(self):
|
||||
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue