@@ -1920,7 +1920,6 @@ static PyObject *
19201920_winapi_PeekNamedPipe_impl (PyObject * module , HANDLE handle , int size )
19211921/*[clinic end generated code: output=d0c3e29e49d323dd input=c7aa53bfbce69d70]*/
19221922{
1923- PyObject * buf = NULL ;
19241923 DWORD nread , navail , nleft ;
19251924 BOOL ret ;
19261925
@@ -1930,20 +1929,26 @@ _winapi_PeekNamedPipe_impl(PyObject *module, HANDLE handle, int size)
19301929 }
19311930
19321931 if (size ) {
1933- buf = PyBytes_FromStringAndSize ( NULL , size );
1934- if (! buf )
1932+ PyBytesWriter * writer = PyBytesWriter_Create ( size );
1933+ if (writer == NULL ) {
19351934 return NULL ;
1935+ }
1936+ char * buf = PyBytesWriter_GetData (writer );
1937+
19361938 Py_BEGIN_ALLOW_THREADS
1937- ret = PeekNamedPipe (handle , PyBytes_AS_STRING ( buf ) , size , & nread ,
1939+ ret = PeekNamedPipe (handle , buf , size , & nread ,
19381940 & navail , & nleft );
19391941 Py_END_ALLOW_THREADS
19401942 if (!ret ) {
1941- Py_DECREF ( buf );
1943+ PyBytesWriter_Discard ( writer );
19421944 return PyErr_SetExcFromWindowsErr (PyExc_OSError , 0 );
19431945 }
1944- if (_PyBytes_Resize (& buf , nread ))
1946+
1947+ PyObject * res = PyBytesWriter_FinishWithSize (writer , nread );
1948+ if (res == NULL ) {
19451949 return NULL ;
1946- return Py_BuildValue ("NII" , buf , navail , nleft );
1950+ }
1951+ return Py_BuildValue ("NII" , res , navail , nleft );
19471952 }
19481953 else {
19491954 Py_BEGIN_ALLOW_THREADS
0 commit comments