@@ -1587,35 +1587,47 @@ static PyObject *
15871587array_array_tofile_impl (arrayobject * self , PyTypeObject * cls , PyObject * f )
15881588/*[clinic end generated code: output=4560c628d9c18bc2 input=5a24da7a7b407b52]*/
15891589{
1590- Py_ssize_t nbytes = Py_SIZE (self ) * self -> ob_descr -> itemsize ;
15911590 /* Write 64K blocks at a time */
15921591 /* XXX Make the block size settable */
15931592 int BLOCKSIZE = 64 * 1024 ;
1594- Py_ssize_t nblocks = (nbytes + BLOCKSIZE - 1 ) / BLOCKSIZE ;
1595- Py_ssize_t i ;
1593+ Py_ssize_t offset = 0 ;
15961594
15971595 if (Py_SIZE (self ) == 0 )
15981596 goto done ;
15991597
1600-
16011598 array_state * state = get_array_state_by_class (cls );
16021599 assert (state != NULL );
16031600
1604- for (i = 0 ; i < nblocks ; i ++ ) {
1605- char * ptr = self -> ob_item + i * BLOCKSIZE ;
1601+ while (1 ) {
1602+ if (self -> ob_item == NULL || Py_SIZE (self ) == 0 ) {
1603+ break ;
1604+ }
1605+
1606+ Py_ssize_t current_nbytes = Py_SIZE (self ) * self -> ob_descr -> itemsize ;
1607+
1608+ if (offset >= current_nbytes ) {
1609+ break ;
1610+ }
1611+
16061612 Py_ssize_t size = BLOCKSIZE ;
1613+ if (offset + size > current_nbytes ) {
1614+ size = current_nbytes - offset ;
1615+ }
1616+
1617+ char * ptr = self -> ob_item + offset ;
16071618 PyObject * bytes , * res ;
16081619
1609- if (i * BLOCKSIZE + size > nbytes )
1610- size = nbytes - i * BLOCKSIZE ;
16111620 bytes = PyBytes_FromStringAndSize (ptr , size );
16121621 if (bytes == NULL )
16131622 return NULL ;
1623+
16141624 res = PyObject_CallMethodOneArg (f , state -> str_write , bytes );
16151625 Py_DECREF (bytes );
16161626 if (res == NULL )
16171627 return NULL ;
1618- Py_DECREF (res ); /* drop write result */
1628+ Py_DECREF (res );
1629+
1630+ offset += size ;
16191631 }
16201632
16211633 done :
0 commit comments