Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix tupleiter_reduce for test_iter's assumptions, and make tupleiter_…
…len and

tupleiter_setstate appropriately relaxed.
  • Loading branch information
Yhg1s committed Jan 10, 2025
commit daacf8202a1778073e2cd1b4f4a781a9fa176c41
17 changes: 14 additions & 3 deletions Objects/tupleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1039,8 +1039,14 @@ tupleiter_len(PyObject *self, PyObject *Py_UNUSED(ignored))
{
_PyTupleIterObject *it = _PyTupleIterObject_CAST(self);
Py_ssize_t len = 0;
#ifdef Py_GIL_DISABLED
Py_ssize_t idx = FT_ATOMIC_LOAD_SSIZE_RELAXED(it->it_index);
if (it->it_seq && idx >= 0 && idx < PyTuple_GET_SIZE(it->it_seq))
Comment thread
Yhg1s marked this conversation as resolved.
Outdated
len = PyTuple_GET_SIZE(it->it_seq) - idx;
#else
if (it->it_seq)
len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
#endif
return PyLong_FromSsize_t(len);
}

Expand All @@ -1056,10 +1062,15 @@ tupleiter_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
* see issue #101765 */
_PyTupleIterObject *it = _PyTupleIterObject_CAST(self);

#ifdef Py_GIL_DISABLED
Py_ssize_t idx = FT_ATOMIC_LOAD_SSIZE_RELAXED(it->it_index);
if (it->it_seq && idx >= 0 && idx < PyTuple_GET_SIZE(it->it_seq))
return Py_BuildValue("N(O)n", iter, it->it_seq, idx);
#else
if (it->it_seq)
return Py_BuildValue("N(O)n", iter, it->it_seq, it->it_index);
else
return Py_BuildValue("N(())", iter);
#endif
return Py_BuildValue("N(())", iter);
}

static PyObject *
Expand All @@ -1074,7 +1085,7 @@ tupleiter_setstate(PyObject *self, PyObject *state)
index = 0;
else if (index > PyTuple_GET_SIZE(it->it_seq))
index = PyTuple_GET_SIZE(it->it_seq); /* exhausted iterator */
it->it_index = index;
FT_ATOMIC_STORE_SSIZE_RELAXED(it->it_index, index);
}
Py_RETURN_NONE;
}
Expand Down