https://github.com/python/cpython/commit/9f205da816fbfff43d8eb9cc3e55fefdf4fad366
commit: 9f205da816fbfff43d8eb9cc3e55fefdf4fad366
branch: main
author: Maurycy Pawłowski-Wieroński <[email protected]>
committer: vstinner <[email protected]>
date: 2026-11-19T10:52:25+01:00
summary:

gh-129813: Use `PyBytesWriter` for strided mmap slices (#162257)

files:
M Modules/mmapmodule.c

diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c
index ce5c59cb021270..06099a3b8b2e0b 100644
--- a/Modules/mmapmodule.c
+++ b/Modules/mmapmodule.c
@@ -1567,22 +1567,16 @@ mmap_subscript_impl(mmap_object *self, PyObject *item)
         else if (step == 1)
             return _safe_PyBytes_FromStringAndSize(self->data + start, 
slicelen);
         else {
-            char *result_buf = (char *)PyMem_Malloc(slicelen);
-            PyObject *result;
-
-            if (result_buf == NULL)
-                return PyErr_NoMemory();
-
-            if (safe_copy_to_slice(result_buf, self->data, start, step,
-                                   slicelen) < 0)
-            {
-                result = NULL;
+            PyBytesWriter *writer = PyBytesWriter_Create(slicelen);
+            if (writer == NULL) {
+                return NULL;
             }
-            else {
-                result = PyBytes_FromStringAndSize(result_buf, slicelen);
+            if (safe_copy_to_slice(PyBytesWriter_GetData(writer), self->data,
+                                   start, step, slicelen) < 0) {
+                PyBytesWriter_Discard(writer);
+                return NULL;
             }
-            PyMem_Free(result_buf);
-            return result;
+            return PyBytesWriter_Finish(writer);
         }
     }
     else {

_______________________________________________
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