Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Lib/test/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,20 @@ def test_reversed_pickle(self):
a[:] = data
self.assertEqual(list(it), [])

def test_exhausted_iterator_setstate(self):
# gh-129139: __setstate__ on an exhausted iterator must not revive it
it = iter([1, 2, 3])
list(it) # exhaust the iterator
it.__setstate__(0)
self.assertRaises(StopIteration, next, it)

def test_exhausted_reversed_iterator_setstate(self):
# gh-129139: __setstate__ on an exhausted reversed iterator must not revive it
it = reversed([1, 2, 3])
list(it) # exhaust the iterator
it.__setstate__(0)
self.assertRaises(StopIteration, next, it)

def test_step_overflow(self):
a = [0, 1, 2, 3, 4]
a[1::sys.maxsize] = [0]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
In free-threaded build, fix ``__setstate__`` on exhausted list iterators and reversed list iterators reviving the iterator instead of being a no-op.
4 changes: 2 additions & 2 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -4108,7 +4108,7 @@ listiter_setstate(PyObject *self, PyObject *state)
Py_ssize_t index = PyLong_AsSsize_t(state);
if (index == -1 && PyErr_Occurred())
return NULL;
if (it->it_seq != NULL) {
if (it->it_seq != NULL && FT_ATOMIC_LOAD_SSIZE_RELAXED(it->it_index) >= 0) {
if (index < -1)
index = -1;
else if (index > PyList_GET_SIZE(it->it_seq))
Expand Down Expand Up @@ -4260,7 +4260,7 @@ listreviter_setstate(PyObject *self, PyObject *state)
Py_ssize_t index = PyLong_AsSsize_t(state);
if (index == -1 && PyErr_Occurred())
return NULL;
if (it->it_seq != NULL) {
if (it->it_seq != NULL && FT_ATOMIC_LOAD_SSIZE_RELAXED(it->it_index) >= 0) {
if (index < -1)
index = -1;
else if (index > PyList_GET_SIZE(it->it_seq) - 1)
Expand Down
Loading