Skip to content

Commit 77a22ef

Browse files
authored
gh-129813, PEP 782: Use PyBytesWriter in _multiprocessing (#139047)
Replace PyBytes_FromStringAndSize(NULL, size) and _PyBytes_Resize() with the new public PyBytesWriter API. Change also 'read' variable type from int to Py_ssize_t.
1 parent 2632426 commit 77a22ef

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

Modules/_multiprocessing/multiprocessing.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,23 +109,22 @@ static PyObject *
109109
_multiprocessing_recv_impl(PyObject *module, HANDLE handle, int size)
110110
/*[clinic end generated code: output=92322781ba9ff598 input=6a5b0834372cee5b]*/
111111
{
112-
int nread;
113-
PyObject *buf;
114-
115-
buf = PyBytes_FromStringAndSize(NULL, size);
116-
if (!buf)
112+
PyBytesWriter *writer = PyBytesWriter_Create(size);
113+
if (!writer) {
117114
return NULL;
115+
}
116+
char *buf = PyBytesWriter_GetData(writer);
118117

118+
Py_ssize_t nread;
119119
Py_BEGIN_ALLOW_THREADS
120-
nread = recv((SOCKET) handle, PyBytes_AS_STRING(buf), size, 0);
120+
nread = recv((SOCKET) handle, buf, size, 0);
121121
Py_END_ALLOW_THREADS
122122

123123
if (nread < 0) {
124-
Py_DECREF(buf);
124+
PyBytesWriter_Discard(writer);
125125
return PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError());
126126
}
127-
_PyBytes_Resize(&buf, nread);
128-
return buf;
127+
return PyBytesWriter_FinishWithSize(writer, nread);
129128
}
130129

131130
/*[clinic input]

0 commit comments

Comments
 (0)