gh-129813, PEP 782: Use PyBytesWriter in _winapi.PeekNamedPipe() (#138930)
Replace PyBytes_FromStringAndSize(NULL, size) and _PyBytes_Resize() with the new public PyBytesWriter API.
This commit is contained in:
@@ -1920,7 +1920,6 @@ static PyObject *
|
|||||||
_winapi_PeekNamedPipe_impl(PyObject *module, HANDLE handle, int size)
|
_winapi_PeekNamedPipe_impl(PyObject *module, HANDLE handle, int size)
|
||||||
/*[clinic end generated code: output=d0c3e29e49d323dd input=c7aa53bfbce69d70]*/
|
/*[clinic end generated code: output=d0c3e29e49d323dd input=c7aa53bfbce69d70]*/
|
||||||
{
|
{
|
||||||
PyObject *buf = NULL;
|
|
||||||
DWORD nread, navail, nleft;
|
DWORD nread, navail, nleft;
|
||||||
BOOL ret;
|
BOOL ret;
|
||||||
|
|
||||||
@@ -1930,20 +1929,26 @@ _winapi_PeekNamedPipe_impl(PyObject *module, HANDLE handle, int size)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (size) {
|
if (size) {
|
||||||
buf = PyBytes_FromStringAndSize(NULL, size);
|
PyBytesWriter *writer = PyBytesWriter_Create(size);
|
||||||
if (!buf)
|
if (writer == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
char *buf = PyBytesWriter_GetData(writer);
|
||||||
|
|
||||||
Py_BEGIN_ALLOW_THREADS
|
Py_BEGIN_ALLOW_THREADS
|
||||||
ret = PeekNamedPipe(handle, PyBytes_AS_STRING(buf), size, &nread,
|
ret = PeekNamedPipe(handle, buf, size, &nread,
|
||||||
&navail, &nleft);
|
&navail, &nleft);
|
||||||
Py_END_ALLOW_THREADS
|
Py_END_ALLOW_THREADS
|
||||||
if (!ret) {
|
if (!ret) {
|
||||||
Py_DECREF(buf);
|
PyBytesWriter_Discard(writer);
|
||||||
return PyErr_SetExcFromWindowsErr(PyExc_OSError, 0);
|
return PyErr_SetExcFromWindowsErr(PyExc_OSError, 0);
|
||||||
}
|
}
|
||||||
if (_PyBytes_Resize(&buf, nread))
|
|
||||||
|
PyObject *res = PyBytesWriter_FinishWithSize(writer, nread);
|
||||||
|
if (res == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
return Py_BuildValue("NII", buf, navail, nleft);
|
}
|
||||||
|
return Py_BuildValue("NII", res, navail, nleft);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Py_BEGIN_ALLOW_THREADS
|
Py_BEGIN_ALLOW_THREADS
|
||||||
|
|||||||
Reference in New Issue
Block a user