66#include "pycore_ceval.h" // _PyEval_GetBuiltin()
77#include "pycore_genobject.h" // _PyCoro_GetAwaitableIter()
88#include "pycore_object.h" // _PyObject_GC_TRACK()
9+ #include "pycore_pyatomic_ft_wrappers.h" // FT_ATOMIC_LOAD_SSIZE_RELAXED()
910
1011
1112typedef struct {
1213 PyObject_HEAD
13- Py_ssize_t it_index ;
14- PyObject * it_seq ; /* Set to NULL when iterator is exhausted */
14+ Py_ssize_t it_index ; /* -1 when iterator is exhausted */
15+ PyObject * it_seq ; /* Set to NULL when iterator is exhausted
16+ (in the default build) */
1517} seqiterobject ;
1618
1719PyObject *
@@ -58,26 +60,41 @@ iter_iternext(PyObject *iterator)
5860
5961 assert (PySeqIter_Check (iterator ));
6062 it = (seqiterobject * )iterator ;
63+ Py_ssize_t index = FT_ATOMIC_LOAD_SSIZE_RELAXED (it -> it_index );
64+ if (index < 0 )
65+ return NULL ;
6166 seq = it -> it_seq ;
67+ #ifndef Py_GIL_DISABLED
6268 if (seq == NULL )
6369 return NULL ;
64- if (it -> it_index == PY_SSIZE_T_MAX ) {
70+ #endif
71+ if (index == PY_SSIZE_T_MAX ) {
6572 PyErr_SetString (PyExc_OverflowError ,
6673 "iter index too large" );
6774 return NULL ;
6875 }
6976
70- result = PySequence_GetItem (seq , it -> it_index );
77+ result = PySequence_GetItem (seq , index );
7178 if (result != NULL ) {
72- it -> it_index ++ ;
79+ /* PySequence_GetItem() can exhaust the iterator re-entrantly.
80+ * Preserve the exhaustion sentinel if it is observed. Concurrent
81+ * exhaustion can still race with the store, but remains memory-safe
82+ * because the sequence stays alive. */
83+ if (FT_ATOMIC_LOAD_SSIZE_RELAXED (it -> it_index ) >= 0 ) {
84+ FT_ATOMIC_STORE_SSIZE_RELAXED (it -> it_index , index + 1 );
85+ }
7386 return result ;
7487 }
7588 if (PyErr_ExceptionMatches (PyExc_IndexError ) ||
7689 PyErr_ExceptionMatches (PyExc_StopIteration ))
7790 {
91+ /* Mark the iterator exhausted before anything that can run
92+ * arbitrary code. */
93+ FT_ATOMIC_STORE_SSIZE_RELAXED (it -> it_index , -1 );
94+ #ifndef Py_GIL_DISABLED
95+ Py_CLEAR (it -> it_seq );
96+ #endif
7897 PyErr_Clear ();
79- it -> it_seq = NULL ;
80- Py_DECREF (seq );
8198 }
8299 return NULL ;
83100}
@@ -88,7 +105,8 @@ iter_len(PyObject *op, PyObject *Py_UNUSED(ignored))
88105 seqiterobject * it = (seqiterobject * )op ;
89106 Py_ssize_t seqsize , len ;
90107
91- if (it -> it_seq ) {
108+ Py_ssize_t index = FT_ATOMIC_LOAD_SSIZE_RELAXED (it -> it_index );
109+ if (index >= 0 && it -> it_seq != NULL ) {
92110 if (_PyObject_HasLen (it -> it_seq )) {
93111 seqsize = PySequence_Size (it -> it_seq );
94112 if (seqsize == -1 )
@@ -97,7 +115,7 @@ iter_len(PyObject *op, PyObject *Py_UNUSED(ignored))
97115 else {
98116 Py_RETURN_NOTIMPLEMENTED ;
99117 }
100- len = seqsize - it -> it_index ;
118+ len = seqsize - index ;
101119 if (len >= 0 )
102120 return PyLong_FromSsize_t (len );
103121 }
@@ -116,8 +134,9 @@ iter_reduce(PyObject *op, PyObject *Py_UNUSED(ignored))
116134 * call must be before access of iterator pointers.
117135 * see issue #101765 */
118136
119- if (it -> it_seq != NULL )
120- return Py_BuildValue ("N(O)n" , iter , it -> it_seq , it -> it_index );
137+ Py_ssize_t index = FT_ATOMIC_LOAD_SSIZE_RELAXED (it -> it_index );
138+ if (index >= 0 && it -> it_seq != NULL )
139+ return Py_BuildValue ("N(O)n" , iter , it -> it_seq , index );
121140 else
122141 return Py_BuildValue ("N(())" , iter );
123142}
@@ -131,10 +150,10 @@ iter_setstate(PyObject *op, PyObject *state)
131150 Py_ssize_t index = PyLong_AsSsize_t (state );
132151 if (index == -1 && PyErr_Occurred ())
133152 return NULL ;
134- if (it -> it_seq != NULL ) {
135- if ( index < 0 )
136- index = 0 ;
137- it -> it_index = index ;
153+ if (index < 0 )
154+ index = 0 ;
155+ if ( it -> it_seq && FT_ATOMIC_LOAD_SSIZE_RELAXED ( it -> it_index ) > = 0 ) {
156+ FT_ATOMIC_STORE_SSIZE_RELAXED ( it -> it_index , index ) ;
138157 }
139158 Py_RETURN_NONE ;
140159}
0 commit comments