https://github.com/python/cpython/commit/82e1920a014cbe38bbadce8544b92e6894bc679b
commit: 82e1920a014cbe38bbadce8544b92e6894bc679b
branch: main
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2025-09-17T17:55:30+02:00
summary:

gh-129813, PEP 782: Use PyBytesWriter in _testclinic (#139048)

Replace PyBytes_FromStringAndSize(NULL, size) with the new public
PyBytesWriter API.

files:
M Modules/_testclinic.c

diff --git a/Modules/_testclinic.c b/Modules/_testclinic.c
index 64cefdc7f0b8e4..69adf7d1a0a950 100644
--- a/Modules/_testclinic.c
+++ b/Modules/_testclinic.c
@@ -663,16 +663,16 @@ str_converter_encoding_impl(PyObject *module, char *a, 
char *b, char *c,
 static PyObject *
 bytes_from_buffer(Py_buffer *buf)
 {
-    PyObject *bytes_obj = PyBytes_FromStringAndSize(NULL, buf->len);
-    if (!bytes_obj) {
+    PyBytesWriter *writer = PyBytesWriter_Create(buf->len);
+    if (writer == NULL) {
         return NULL;
     }
-    void *bytes_obj_buf = ((PyBytesObject *)bytes_obj)->ob_sval;
-    if (PyBuffer_ToContiguous(bytes_obj_buf, buf, buf->len, 'C') < 0) {
-        Py_DECREF(bytes_obj);
+    void *data = PyBytesWriter_GetData(writer);
+    if (PyBuffer_ToContiguous(data, buf, buf->len, 'C') < 0) {
+        PyBytesWriter_Discard(writer);
         return NULL;
     }
-    return bytes_obj;
+    return PyBytesWriter_Finish(writer);
 }
 
 /*[clinic input]

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to