pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/python/cpython/commit/02437641d20f58191558ba52570832ef3a30cfef

8faa60c69660fa.css" /> bpo-45018: Fix rangeiter_reduce in rangeobject.c (GH-27938) (GH-27991) · python/cpython@0243764 · GitHub
Skip to content

Commit 0243764

Browse files
bpo-45018: Fix rangeiter_reduce in rangeobject.c (GH-27938) (GH-27991)
Co-authored-by: Łukasz Langa <lukasz@langa.pl> (cherry picked from commit 94a3d2a) Co-authored-by: chilaxan <chilaxan@gmail.com>
1 parent f1e3fc4 commit 0243764

3 files changed

Lines changed: 33 additions & 17 deletions

File tree

Lib/test/test_range.py

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -374,26 +374,41 @@ def test_pickling(self):
374374
list(r))
375375

376376
def test_iterator_pickling(self):
377-
testcases = [(13,), (0, 11), (-22, 10), (20, 3, -1),
378-
(13, 21, 3), (-2, 2, 2), (2**65, 2**65+2)]
377+
testcases = [(13,), (0, 11), (-22, 10), (20, 3, -1), (13, 21, 3),
378+
(-2, 2, 2), (2**31-3, 2**31-1), (2**33, 2**33+2),
379+
(2**63-3, 2**63-1), (2**65, 2**65+2)]
379380
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
380381
for t in testcases:
381-
it = itorg = iter(range(*t))
382-
data = list(range(*t))
383-
384-
d = pickle.dumps(it, proto)
385-
it = pickle.loads(d)
386-
self.assertEqual(type(itorg), type(it))
387-
self.assertEqual(list(it), data)
388-
389-
it = pickle.loads(d)
390-
try:
391-
next(it)
392-
except StopIteration:
393-
continue
382+
with self.subTest(proto=proto, t=t):
383+
it = itorg = iter(range(*t))
384+
data = list(range(*t))
385+
386+
d = pickle.dumps(it, proto)
387+
it = pickle.loads(d)
388+
self.assertEqual(type(itorg), type(it))
389+
self.assertEqual(list(it), data)
390+
391+
it = pickle.loads(d)
392+
try:
393+
next(it)
394+
except StopIteration:
395+
continue
396+
d = pickle.dumps(it, proto)
397+
it = pickle.loads(d)
398+
self.assertEqual(list(it), data[1:])
399+
400+
def test_iterator_pickling_overflowing_index(self):
401+
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
402+
with self.subTest(proto=proto):
403+
it = iter(range(2**32 + 2))
404+
_, _, idx = it.__reduce__()
405+
self.assertEqual(idx, 0)
406+
it.__setstate__(2**32 + 1) # undocumented way to set r->index
407+
_, _, idx = it.__reduce__()
408+
self.assertEqual(idx, 2**32 + 1)
394409
d = pickle.dumps(it, proto)
395410
it = pickle.loads(d)
396-
self.assertEqual(list(it), data[1:])
411+
self.assertEqual(next(it), 2**32 + 1)
397412

398413
def test_exhausted_iterator_pickling(self):
399414
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed pickling of range iterators that iterated for over 2**32 times.

Objects/rangeobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ rangeiter_reduce(rangeiterobject *r, PyObject *Py_UNUSED(ignored))
793793
if (range == NULL)
794794
goto err;
795795
/* return the result */
796-
return Py_BuildValue("N(N)i", _PyEval_GetBuiltinId(&PyId_iter),
796+
return Py_BuildValue("N(N)l", _PyEval_GetBuiltinId(&PyId_iter),
797797
range, r->index);
798798
err:
799799
Py_XDECREF(start);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy