https://github.com/python/cpython/commit/703da5e81db012713228414b02b39d3240dc1d02
commit: 703da5e81db012713228414b02b39d3240dc1d02
branch: main
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2025-09-13T18:36:05+02:00
summary:

gh-129813, PEP 782: Use PyBytesWriter in memoryview (#138836)

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

files:
M Objects/memoryobject.c

diff --git a/Objects/memoryobject.c b/Objects/memoryobject.c
index 94ff0fe624e61e..f1232f389210ea 100644
--- a/Objects/memoryobject.c
+++ b/Objects/memoryobject.c
@@ -2285,7 +2285,6 @@ memoryview_tobytes_impl(PyMemoryViewObject *self, const 
char *order)
 {
     Py_buffer *src = VIEW_ADDR(self);
     char ord = 'C';
-    PyObject *bytes;
 
     CHECK_RELEASED(self);
 
@@ -2303,16 +2302,18 @@ memoryview_tobytes_impl(PyMemoryViewObject *self, const 
char *order)
         }
     }
 
-    bytes = PyBytes_FromStringAndSize(NULL, src->len);
-    if (bytes == NULL)
+    PyBytesWriter *writer = PyBytesWriter_Create(src->len);
+    if (writer == NULL) {
         return NULL;
+    }
 
-    if (PyBuffer_ToContiguous(PyBytes_AS_STRING(bytes), src, src->len, ord) < 
0) {
-        Py_DECREF(bytes);
+    if (PyBuffer_ToContiguous(PyBytesWriter_GetData(writer),
+                              src, src->len, ord) < 0) {
+        PyBytesWriter_Discard(writer);
         return NULL;
     }
 
-    return bytes;
+    return PyBytesWriter_Finish(writer);
 }
 
 /*[clinic input]
@@ -2344,8 +2345,6 @@ memoryview_hex_impl(PyMemoryViewObject *self, PyObject 
*sep,
 /*[clinic end generated code: output=430ca760f94f3ca7 input=539f6a3a5fb56946]*/
 {
     Py_buffer *src = VIEW_ADDR(self);
-    PyObject *bytes;
-    PyObject *ret;
 
     CHECK_RELEASED(self);
 
@@ -2353,19 +2352,22 @@ memoryview_hex_impl(PyMemoryViewObject *self, PyObject 
*sep,
         return _Py_strhex_with_sep(src->buf, src->len, sep, bytes_per_sep);
     }
 
-    bytes = PyBytes_FromStringAndSize(NULL, src->len);
-    if (bytes == NULL)
+    PyBytesWriter *writer = PyBytesWriter_Create(src->len);
+    if (writer == NULL) {
         return NULL;
+    }
 
-    if (PyBuffer_ToContiguous(PyBytes_AS_STRING(bytes), src, src->len, 'C') < 
0) {
-        Py_DECREF(bytes);
+    if (PyBuffer_ToContiguous(PyBytesWriter_GetData(writer),
+                              src, src->len, 'C') < 0) {
+        PyBytesWriter_Discard(writer);
         return NULL;
     }
 
-    ret = _Py_strhex_with_sep(
-            PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes),
-            sep, bytes_per_sep);
-    Py_DECREF(bytes);
+    PyObject *ret = _Py_strhex_with_sep(
+        PyBytesWriter_GetData(writer),
+        PyBytesWriter_GetSize(writer),
+        sep, bytes_per_sep);
+    PyBytesWriter_Discard(writer);
 
     return ret;
 }

_______________________________________________
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