Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c947a46

Browse files
authored
gh-155742: Use PyBytesWriter in winconsoleio.c (#157391)
Replace soft deprecated _PyBytes_Resize() with PyBytesWriter.
1 parent 8a2db99 commit c947a46

1 file changed

Lines changed: 7 additions & 14 deletions

File tree

Modules/_io/winconsoleio.c

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,6 @@ _io__WindowsConsoleIO_read_impl(winconsoleio *self, PyTypeObject *cls,
10091009
Py_ssize_t size)
10101010
/*[clinic end generated code: output=7e569a586537c0ae input=a14570a5da273365]*/
10111011
{
1012-
PyObject *bytes;
10131012
Py_ssize_t bytes_size;
10141013

10151014
if (self->fd == -1)
@@ -1026,26 +1025,20 @@ _io__WindowsConsoleIO_read_impl(winconsoleio *self, PyTypeObject *cls,
10261025
return NULL;
10271026
}
10281027

1029-
bytes = PyBytes_FromStringAndSize(NULL, size);
1030-
if (bytes == NULL)
1028+
PyBytesWriter *writer = PyBytesWriter_Create(size);
1029+
if (writer == NULL) {
10311030
return NULL;
1031+
}
10321032

10331033
_PyIO_State *state = get_io_state_by_cls(cls);
1034-
bytes_size = readinto(state, self, PyBytes_AS_STRING(bytes),
1035-
PyBytes_GET_SIZE(bytes));
1034+
bytes_size = readinto(state, self, PyBytesWriter_GetData(writer),
1035+
PyBytesWriter_GetSize(writer));
10361036
if (bytes_size < 0) {
1037-
Py_CLEAR(bytes);
1037+
PyBytesWriter_Discard(writer);
10381038
return NULL;
10391039
}
10401040

1041-
if (bytes_size < PyBytes_GET_SIZE(bytes)) {
1042-
if (_PyBytes_Resize(&bytes, bytes_size) < 0) {
1043-
Py_CLEAR(bytes);
1044-
return NULL;
1045-
}
1046-
}
1047-
1048-
return bytes;
1041+
return PyBytesWriter_FinishWithSize(writer, bytes_size);
10491042
}
10501043

10511044
/*[clinic input]

0 commit comments

Comments
 (0)