https://github.com/python/cpython/commit/cbeada9ec76e6edd7939eef95af0db35faeae50c
commit: cbeada9ec76e6edd7939eef95af0db35faeae50c
branch: 3.13
author: Sergey Miryanov <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2025-11-15T21:14:17+02:00
summary:
[3.13] GH-141312: Allow only integers to longrangeiter_setstate state
(GH-141317) (GH-141568)
This fixes an assertion error when the new computed start is not an integer.
(cherry picked from commit 10bec7c1eb3ee27f490a067426eef452b15f78f9)
files:
A Misc/NEWS.d/next/Core and
Builtins/2025-11-10-23-07-06.gh-issue-141312.H-58GB.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 3870b153688b25..2c9c290e8906b7 100644
--- a/Lib/test/test_range.py
+++ b/Lib/test/test_range.py
@@ -470,6 +470,16 @@ def test_iterator_setstate(self):
it.__setstate__(2**64 - 7)
self.assertEqual(list(it), [12, 10])
+ def test_iterator_invalid_setstate(self):
+ for invalid_value in (1.0, ""):
+ ranges = (('rangeiter', range(10, 100, 2)),
+ ('longrangeiter', range(10, 2**65, 2)))
+ for rng_name, rng in ranges:
+ with self.subTest(invalid_value=invalid_value, range=rng_name):
+ it = iter(rng)
+ with self.assertRaises(TypeError):
+ it.__setstate__(invalid_value)
+
def test_odd_bug(self):
# This used to raise a "SystemError: NULL result without error"
# because the range validation step was eating the exception
diff --git a/Misc/NEWS.d/next/Core and
Builtins/2025-11-10-23-07-06.gh-issue-141312.H-58GB.rst b/Misc/NEWS.d/next/Core
and Builtins/2025-11-10-23-07-06.gh-issue-141312.H-58GB.rst
new file mode 100644
index 00000000000000..fdb136cef3f33c
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and
Builtins/2025-11-10-23-07-06.gh-issue-141312.H-58GB.rst
@@ -0,0 +1,2 @@
+Fix the assertion failure in the ``__setstate__`` method of the range iterator
+when a non-integer argument is passed. Patch by Sergey Miryanov.
diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c
index 7da6162744ffd6..57e1e008320572 100644
--- a/Objects/rangeobject.c
+++ b/Objects/rangeobject.c
@@ -1014,6 +1014,11 @@ longrangeiter_reduce(longrangeiterobject *r, PyObject
*Py_UNUSED(ignored))
static PyObject *
longrangeiter_setstate(longrangeiterobject *r, PyObject *state)
{
+ if (!PyLong_CheckExact(state)) {
+ PyErr_Format(PyExc_TypeError, "state must be an int, not %T", state);
+ return NULL;
+ }
+
PyObject *zero = _PyLong_GetZero(); // borrowed reference
int cmp;
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]