[Python-checkins] bpo-45018: Fix rangeiter_reduce in rangeobject.c (GH-27938) (GH-27991)

ambv webhook-mailer at python.org
Fri Aug 27 06:52:16 EDT 2021


https://github.com/python/cpython/commit/02437641d20f58191558ba52570832ef3a30cfef
commit: 02437641d20f58191558ba52570832ef3a30cfef
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: ambv <lukasz at langa.pl>
date: 2021-08-27T12:52:07+02:00
summary:

bpo-45018: Fix rangeiter_reduce in rangeobject.c (GH-27938) (GH-27991)

Co-authored-by: Łukasz Langa <lukasz at langa.pl>
(cherry picked from commit 94a3d2a6329ab7941e93ad2f5bcbb8af2b8b80d2)

Co-authored-by: chilaxan <chilaxan at gmail.com>

files:
A Misc/NEWS.d/next/Core and Builtins/2021-08-26-18-44-03.bpo-45018.pu8H9L.rst
M Lib/test/test_range.py
M Objects/rangeobject.c

diff --git a/Lib/test/test_range.py b/Lib/test/test_range.py
index 30fa129b50ecb..16b0fc060aaed 100644
--- a/Lib/test/test_range.py
+++ b/Lib/test/test_range.py
@@ -374,26 +374,41 @@ def test_pickling(self):
                                      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))
-
-                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
+                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(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):
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-08-26-18-44-03.bpo-45018.pu8H9L.rst b/Misc/NEWS.d/next/Core and Builtins/2021-08-26-18-44-03.bpo-45018.pu8H9L.rst
new file mode 100644
index 0000000000000..5bf13ef06f34c
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2021-08-26-18-44-03.bpo-45018.pu8H9L.rst	
@@ -0,0 +1 @@
+Fixed pickling of range iterators that iterated for over 2**32 times.
diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c
index 751dbb9815d82..dd027936589ac 100644
--- a/Objects/rangeobject.c
+++ b/Objects/rangeobject.c
@@ -793,7 +793,7 @@ rangeiter_reduce(rangeiterobject *r, PyObject *Py_UNUSED(ignored))
     if (range == NULL)
         goto err;
     /* return the result */
-    return Py_BuildValue("N(N)i", _PyEval_GetBuiltinId(&PyId_iter),
+    return Py_BuildValue("N(N)l", _PyEval_GetBuiltinId(&PyId_iter),
                          range, r->index);
 err:
     Py_XDECREF(start);



More information about the Python-checkins mailing list