@@ -1026,58 +1026,60 @@ static PyObject *
10261026_io__Buffered_read1_impl (buffered * self , Py_ssize_t n )
10271027/*[clinic end generated code: output=bcc4fb4e54d103a3 input=3d0ad241aa52b36c]*/
10281028{
1029- Py_ssize_t have , r ;
1030- PyObject * res = NULL ;
1031-
10321029 CHECK_INITIALIZED (self )
10331030 if (n < 0 ) {
10341031 n = self -> buffer_size ;
10351032 }
10361033
10371034 CHECK_CLOSED (self , "read of closed file" )
10381035
1039- if (n == 0 )
1040- return PyBytes_FromStringAndSize (NULL , 0 );
1036+ if (n == 0 ) {
1037+ return Py_GetConstant (Py_CONSTANT_EMPTY_BYTES );
1038+ }
10411039
10421040 /* Return up to n bytes. If at least one byte is buffered, we
10431041 only return buffered bytes. Otherwise, we do one raw read. */
10441042
1045- have = Py_SAFE_DOWNCAST (READAHEAD (self ), Py_off_t , Py_ssize_t );
1043+ Py_ssize_t have = Py_SAFE_DOWNCAST (READAHEAD (self ), Py_off_t , Py_ssize_t );
10461044 if (have > 0 ) {
10471045 n = Py_MIN (have , n );
1048- res = _bufferedreader_read_fast (self , n );
1046+ PyObject * res = _bufferedreader_read_fast (self , n );
10491047 assert (res != Py_None );
10501048 return res ;
10511049 }
1052- res = PyBytes_FromStringAndSize (NULL , n );
1053- if (res == NULL )
1054- return NULL ;
1050+
10551051 if (!ENTER_BUFFERED (self )) {
1056- Py_DECREF (res );
10571052 return NULL ;
10581053 }
1054+
10591055 /* Flush the write buffer if necessary */
10601056 if (self -> writable ) {
1061- PyObject * r = buffered_flush_and_rewind_unlocked (self );
1062- if (r == NULL ) {
1057+ PyObject * res = buffered_flush_and_rewind_unlocked (self );
1058+ if (res == NULL ) {
10631059 LEAVE_BUFFERED (self )
1064- Py_DECREF (res );
10651060 return NULL ;
10661061 }
1067- Py_DECREF (r );
1062+ Py_DECREF (res );
10681063 }
10691064 _bufferedreader_reset_buf (self );
1070- r = _bufferedreader_raw_read (self , PyBytes_AS_STRING (res ), n );
1065+
1066+ PyBytesWriter * writer = PyBytesWriter_Create (n );
1067+ if (writer == NULL ) {
1068+ return NULL ;
1069+ }
1070+
1071+ Py_ssize_t r = _bufferedreader_raw_read (self ,
1072+ PyBytesWriter_GetData (writer ), n );
10711073 LEAVE_BUFFERED (self )
10721074 if (r == -1 ) {
1073- Py_DECREF ( res );
1075+ PyBytesWriter_Discard ( writer );
10741076 return NULL ;
10751077 }
1076- if (r == -2 )
1078+ if (r == -2 ) {
10771079 r = 0 ;
1078- if ( n > r )
1079- _PyBytes_Resize ( & res , r );
1080- return res ;
1080+ }
1081+
1082+ return PyBytesWriter_FinishWithSize ( writer , r ) ;
10811083}
10821084
10831085static PyObject *
0 commit comments