@@ -1026,58 +1026,59 @@ 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 )
1050+
1051+ PyBytesWriter * writer = PyBytesWriter_Create (n );
1052+ if (writer == NULL ) {
10541053 return NULL ;
1054+ }
10551055 if (!ENTER_BUFFERED (self )) {
1056- Py_DECREF ( res );
1056+ PyBytesWriter_Discard ( writer );
10571057 return NULL ;
10581058 }
10591059 /* Flush the write buffer if necessary */
10601060 if (self -> writable ) {
1061- PyObject * r = buffered_flush_and_rewind_unlocked (self );
1062- if (r == NULL ) {
1061+ PyObject * res = buffered_flush_and_rewind_unlocked (self );
1062+ if (res == NULL ) {
10631063 LEAVE_BUFFERED (self )
1064- Py_DECREF ( res );
1064+ PyBytesWriter_Discard ( writer );
10651065 return NULL ;
10661066 }
1067- Py_DECREF (r );
1067+ Py_DECREF (res );
10681068 }
10691069 _bufferedreader_reset_buf (self );
1070- r = _bufferedreader_raw_read (self , PyBytes_AS_STRING (res ), n );
1070+ Py_ssize_t r = _bufferedreader_raw_read (self ,
1071+ PyBytesWriter_GetData (writer ), n );
10711072 LEAVE_BUFFERED (self )
10721073 if (r == -1 ) {
1073- Py_DECREF ( res );
1074+ PyBytesWriter_Discard ( writer );
10741075 return NULL ;
10751076 }
1076- if (r == -2 )
1077+ if (r == -2 ) {
10771078 r = 0 ;
1078- if ( n > r )
1079- _PyBytes_Resize ( & res , r );
1080- return res ;
1079+ }
1080+
1081+ return PyBytesWriter_FinishWithSize ( writer , r ) ;
10811082}
10821083
10831084static PyObject *
0 commit comments