]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-129813, PEP 782: Use PyBytesWriter in _multiprocessing (#139047)
authorVictor Stinner <vstinner@python.org>
Wed, 17 Sep 2025 15:44:13 +0000 (16:44 +0100)
committerGitHub <noreply@github.com>
Wed, 17 Sep 2025 15:44:13 +0000 (17:44 +0200)
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.

Modules/_multiprocessing/multiprocessing.c

index cee8cf7b9a83c0651b79e221edda3e0a49e01f2e..848784dedc17027c8b945dbb818b931eec4f1e76 100644 (file)
@@ -109,23 +109,22 @@ static PyObject *
 _multiprocessing_recv_impl(PyObject *module, HANDLE handle, int size)
 /*[clinic end generated code: output=92322781ba9ff598 input=6a5b0834372cee5b]*/
 {
-    int nread;
-    PyObject *buf;
-
-    buf = PyBytes_FromStringAndSize(NULL, size);
-    if (!buf)
+    PyBytesWriter *writer = PyBytesWriter_Create(size);
+    if (!writer) {
         return NULL;
+    }
+    char *buf = PyBytesWriter_GetData(writer);
 
+    Py_ssize_t nread;
     Py_BEGIN_ALLOW_THREADS
-    nread = recv((SOCKET) handle, PyBytes_AS_STRING(buf), size, 0);
+    nread = recv((SOCKET) handle, buf, size, 0);
     Py_END_ALLOW_THREADS
 
     if (nread < 0) {
-        Py_DECREF(buf);
+        PyBytesWriter_Discard(writer);
         return PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError());
     }
-    _PyBytes_Resize(&buf, nread);
-    return buf;
+    return PyBytesWriter_FinishWithSize(writer, nread);
 }
 
 /*[clinic input]