@@ -3179,7 +3179,7 @@ static int
31793179batch_list_exact (PickleState * state , PicklerObject * self , PyObject * obj )
31803180{
31813181 PyObject * item = NULL ;
3182- Py_ssize_t this_batch , total ;
3182+ Py_ssize_t this_batch , total , list_size ;
31833183
31843184 const char append_op = APPEND ;
31853185 const char appends_op = APPENDS ;
@@ -3189,10 +3189,12 @@ batch_list_exact(PickleState *state, PicklerObject *self, PyObject *obj)
31893189 assert (self -> proto > 0 );
31903190 assert (PyList_CheckExact (obj ));
31913191
3192+ list_size = PyList_GET_SIZE (obj );
3193+
31923194 /* Write in batches of BATCHSIZE. */
31933195 total = 0 ;
31943196 do {
3195- if (PyList_GET_SIZE ( obj ) - total == 1 ) {
3197+ if (list_size - total == 1 ) {
31963198 item = PyList_GetItemRef (obj , total );
31973199 if (item == NULL ) {
31983200 _PyErr_FormatNote ("when serializing %T item %zd" , obj , total );
@@ -3229,6 +3231,12 @@ batch_list_exact(PickleState *state, PicklerObject *self, PyObject *obj)
32293231 }
32303232 if (_Pickler_Write (self , & appends_op , 1 ) < 0 )
32313233 return -1 ;
3234+ if (PyList_GET_SIZE (obj ) != list_size ) {
3235+ PyErr_Format (
3236+ PyExc_RuntimeError ,
3237+ "list changed size during iteration" );
3238+ return -1 ;
3239+ }
32323240
32333241 } while (total < PyList_GET_SIZE (obj ));
32343242
0 commit comments