https://github.com/python/cpython/commit/10bec7c1eb3ee27f490a067426eef452b15f78f9
commit: 10bec7c1eb3ee27f490a067426eef452b15f78f9
branch: main
author: Sergey Miryanov <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2025-11-14T14:52:01Z
summary:
GH-141312: Allow only integers to longrangeiter_setstate state (GH-141317)
This fixes an assertion error when the new computed start is not an integer.
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 f8cdfe68a6435e..e93346fb27703f 100644
--- a/Objects/rangeobject.c
+++ b/Objects/rangeobject.c
@@ -1042,6 +1042,11 @@ longrangeiter_reduce(PyObject *op, PyObject
*Py_UNUSED(ignored))
static PyObject *
longrangeiter_setstate(PyObject *op, PyObject *state)
{
+ if (!PyLong_CheckExact(state)) {
+ PyErr_Format(PyExc_TypeError, "state must be an int, not %T", state);
+ return NULL;
+ }
+
longrangeiterobject *r = (longrangeiterobject*)op;
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]