@@ -1026,58 +1026,60 @@ static PyObject *
1026
1026
_io__Buffered_read1_impl (buffered * self , Py_ssize_t n )
1027
1027
/*[clinic end generated code: output=bcc4fb4e54d103a3 input=3d0ad241aa52b36c]*/
1028
1028
{
1029
- Py_ssize_t have , r ;
1030
- PyObject * res = NULL ;
1031
-
1032
1029
CHECK_INITIALIZED (self )
1033
1030
if (n < 0 ) {
1034
1031
n = self -> buffer_size ;
1035
1032
}
1036
1033
1037
1034
CHECK_CLOSED (self , "read of closed file" )
1038
1035
1039
- if (n == 0 )
1040
- return PyBytes_FromStringAndSize (NULL , 0 );
1036
+ if (n == 0 ) {
1037
+ return Py_GetConstant (Py_CONSTANT_EMPTY_BYTES );
1038
+ }
1041
1039
1042
1040
/* Return up to n bytes. If at least one byte is buffered, we
1043
1041
only return buffered bytes. Otherwise, we do one raw read. */
1044
1042
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 );
1046
1044
if (have > 0 ) {
1047
1045
n = Py_MIN (have , n );
1048
- res = _bufferedreader_read_fast (self , n );
1046
+ PyObject * res = _bufferedreader_read_fast (self , n );
1049
1047
assert (res != Py_None );
1050
1048
return res ;
1051
1049
}
1052
- res = PyBytes_FromStringAndSize (NULL , n );
1053
- if (res == NULL )
1054
- return NULL ;
1050
+
1055
1051
if (!ENTER_BUFFERED (self )) {
1056
- Py_DECREF (res );
1057
1052
return NULL ;
1058
1053
}
1054
+
1059
1055
/* Flush the write buffer if necessary */
1060
1056
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 ) {
1063
1059
LEAVE_BUFFERED (self )
1064
- Py_DECREF (res );
1065
1060
return NULL ;
1066
1061
}
1067
- Py_DECREF (r );
1062
+ Py_DECREF (res );
1068
1063
}
1069
1064
_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 );
1071
1073
LEAVE_BUFFERED (self )
1072
1074
if (r == -1 ) {
1073
- Py_DECREF ( res );
1075
+ PyBytesWriter_Discard ( writer );
1074
1076
return NULL ;
1075
1077
}
1076
- if (r == -2 )
1078
+ if (r == -2 ) {
1077
1079
r = 0 ;
1078
- if ( n > r )
1079
- _PyBytes_Resize ( & res , r );
1080
- return res ;
1080
+ }
1081
+
1082
+ return PyBytesWriter_FinishWithSize ( writer , r ) ;
1081
1083
}
1082
1084
1083
1085
static PyObject *
0 commit comments