@@ -547,14 +547,14 @@ fileio_readinto(fileio *self, PyObject *args)
547547}
548548
549549static size_t
550- new_buffersize (fileio * self , size_t currentsize )
550+ new_buffersize (fileio * self , size_t currentsize
551+ #ifdef HAVE_FSTAT
552+ , off_t pos , off_t end
553+ #endif
554+ )
551555{
552556#ifdef HAVE_FSTAT
553- off_t pos , end ;
554- struct stat st ;
555- if (fstat (self -> fd , & st ) == 0 ) {
556- end = st .st_size ;
557- pos = lseek (self -> fd , 0L , SEEK_CUR );
557+ if (end != (off_t )- 1 ) {
558558 /* Files claiming a size smaller than SMALLCHUNK may
559559 actually be streaming pseudo-files. In this case, we
560560 apply the more aggressive algorithm below.
@@ -579,9 +579,14 @@ new_buffersize(fileio *self, size_t currentsize)
579579static PyObject *
580580fileio_readall (fileio * self )
581581{
582+ #ifdef HAVE_FSTAT
583+ struct stat st ;
584+ off_t pos , end ;
585+ #endif
582586 PyObject * result ;
583587 Py_ssize_t total = 0 ;
584588 int n ;
589+ size_t newsize ;
585590
586591 if (self -> fd < 0 )
587592 return err_closed ();
@@ -592,8 +597,23 @@ fileio_readall(fileio *self)
592597 if (result == NULL )
593598 return NULL ;
594599
600+ #ifdef HAVE_FSTAT
601+ #if defined(MS_WIN64 ) || defined(MS_WINDOWS )
602+ pos = _lseeki64 (self -> fd , 0L , SEEK_CUR );
603+ #else
604+ pos = lseek (self -> fd , 0L , SEEK_CUR );
605+ #endif
606+ if (fstat (self -> fd , & st ) == 0 )
607+ end = st .st_size ;
608+ else
609+ end = (off_t )- 1 ;
610+ #endif
595611 while (1 ) {
596- size_t newsize = new_buffersize (self , total );
612+ #ifdef HAVE_FSTAT
613+ newsize = new_buffersize (self , total , pos , end );
614+ #else
615+ newsize = new_buffersize (self , total );
616+ #endif
597617 if (newsize > PY_SSIZE_T_MAX || newsize <= 0 ) {
598618 PyErr_SetString (PyExc_OverflowError ,
599619 "unbounded read returned more bytes "
@@ -632,6 +652,9 @@ fileio_readall(fileio *self)
632652 return NULL ;
633653 }
634654 total += n ;
655+ #ifdef HAVE_FSTAT
656+ pos += n ;
657+ #endif
635658 }
636659
637660 if (PyBytes_GET_SIZE (result ) > total ) {
0 commit comments