Skip to content

Commit c5fb72e

Browse files
authored
gh-129813, PEP 782: Use PyBytesWriter in _winapi.PeekNamedPipe() (#138930)
Replace PyBytes_FromStringAndSize(NULL, size) and _PyBytes_Resize() with the new public PyBytesWriter API.
1 parent dfd52e7 commit c5fb72e

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

Modules/_winapi.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)