mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
[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 10bec7c1eb)
This commit is contained in:
parent
b693037040
commit
cbeada9ec7
3 changed files with 17 additions and 0 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue