[Python-checkins] bpo-46140: take more Py_buffer arguments as const * (GH-30217)

serhiy-storchaka webhook-mailer at python.org
Wed Dec 22 08:08:03 EST 2021


https://github.com/python/cpython/commit/31ff96712e8f89ac1056c2da880b44650002219f
commit: 31ff96712e8f89ac1056c2da880b44650002219f
branch: main
author: David Hewitt <1939362+davidhewitt at users.noreply.github.com>
committer: serhiy-storchaka <storchaka at gmail.com>
date: 2021-12-22T15:07:46+02:00
summary:

bpo-46140: take more Py_buffer arguments as const * (GH-30217)

files:
A Misc/NEWS.d/next/C API/2021-12-21-22-56-36.bpo-46140.dvXkYK.rst
M Doc/c-api/buffer.rst
M Doc/c-api/memoryview.rst
M Include/cpython/abstract.h
M Include/memoryobject.h
M Objects/abstract.c
M Objects/memoryobject.c

diff --git a/Doc/c-api/buffer.rst b/Doc/c-api/buffer.rst
index e32719373cc71..820a3a6f990ef 100644
--- a/Doc/c-api/buffer.rst
+++ b/Doc/c-api/buffer.rst
@@ -470,27 +470,27 @@ Buffer-related functions
    .. versionadded:: 3.9
 
 
-.. c:function:: int PyBuffer_IsContiguous(Py_buffer *view, char order)
+.. c:function:: int PyBuffer_IsContiguous(const Py_buffer *view, char order)
 
    Return ``1`` if the memory defined by the *view* is C-style (*order* is
    ``'C'``) or Fortran-style (*order* is ``'F'``) :term:`contiguous` or either one
    (*order* is ``'A'``).  Return ``0`` otherwise.  This function always succeeds.
 
 
-.. c:function:: void* PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices)
+.. c:function:: void* PyBuffer_GetPointer(const Py_buffer *view, const Py_ssize_t *indices)
 
    Get the memory area pointed to by the *indices* inside the given *view*.
    *indices* must point to an array of ``view->ndim`` indices.
 
 
-.. c:function:: int PyBuffer_FromContiguous(Py_buffer *view, void *buf, Py_ssize_t len, char fort)
+.. c:function:: int PyBuffer_FromContiguous(const Py_buffer *view, const void *buf, Py_ssize_t len, char fort)
 
    Copy contiguous *len* bytes from *buf* to *view*.
    *fort* can be ``'C'`` or ``'F'`` (for C-style or Fortran-style ordering).
    ``0`` is returned on success, ``-1`` on error.
 
 
-.. c:function:: int PyBuffer_ToContiguous(void *buf, Py_buffer *src, Py_ssize_t len, char order)
+.. c:function:: int PyBuffer_ToContiguous(void *buf, const Py_buffer *src, Py_ssize_t len, char order)
 
    Copy *len* bytes from *src* to its contiguous representation in *buf*.
    *order* can be ``'C'`` or ``'F'`` or ``'A'`` (for C-style or Fortran-style
diff --git a/Doc/c-api/memoryview.rst b/Doc/c-api/memoryview.rst
index 24f8c935302e8..4d94b3f545f32 100644
--- a/Doc/c-api/memoryview.rst
+++ b/Doc/c-api/memoryview.rst
@@ -27,7 +27,7 @@ any other object.
 
    .. versionadded:: 3.3
 
-.. c:function:: PyObject *PyMemoryView_FromBuffer(Py_buffer *view)
+.. c:function:: PyObject *PyMemoryView_FromBuffer(const Py_buffer *view)
 
    Create a memoryview object wrapping the given buffer structure *view*.
    For simple byte buffers, :c:func:`PyMemoryView_FromMemory` is the preferred
@@ -61,4 +61,3 @@ any other object.
    on or ``NULL`` if the memoryview has been created by one of the functions
    :c:func:`PyMemoryView_FromMemory` or :c:func:`PyMemoryView_FromBuffer`.
    *mview* **must** be a memoryview instance.
-
diff --git a/Include/cpython/abstract.h b/Include/cpython/abstract.h
index 55a742c31fada..2876a7bb84f52 100644
--- a/Include/cpython/abstract.h
+++ b/Include/cpython/abstract.h
@@ -183,17 +183,17 @@ PyAPI_FUNC(int) PyObject_GetBuffer(PyObject *obj, Py_buffer *view,
 
 /* Get the memory area pointed to by the indices for the buffer given.
    Note that view->ndim is the assumed size of indices. */
-PyAPI_FUNC(void *) PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices);
+PyAPI_FUNC(void *) PyBuffer_GetPointer(const Py_buffer *view, const Py_ssize_t *indices);
 
 /* Return the implied itemsize of the data-format area from a
    struct-style description. */
 PyAPI_FUNC(Py_ssize_t) PyBuffer_SizeFromFormat(const char *format);
 
 /* Implementation in memoryobject.c */
-PyAPI_FUNC(int) PyBuffer_ToContiguous(void *buf, Py_buffer *view,
+PyAPI_FUNC(int) PyBuffer_ToContiguous(void *buf, const Py_buffer *view,
                                       Py_ssize_t len, char order);
 
-PyAPI_FUNC(int) PyBuffer_FromContiguous(Py_buffer *view, void *buf,
+PyAPI_FUNC(int) PyBuffer_FromContiguous(const Py_buffer *view, const void *buf,
                                         Py_ssize_t len, char order);
 
 /* Copy len bytes of data from the contiguous chunk of memory
diff --git a/Include/memoryobject.h b/Include/memoryobject.h
index 306028f4b225d..0298cc9373068 100644
--- a/Include/memoryobject.h
+++ b/Include/memoryobject.h
@@ -26,7 +26,7 @@ PyAPI_FUNC(PyObject *) PyMemoryView_FromMemory(char *mem, Py_ssize_t size,
                                                int flags);
 #endif
 #ifndef Py_LIMITED_API
-PyAPI_FUNC(PyObject *) PyMemoryView_FromBuffer(Py_buffer *info);
+PyAPI_FUNC(PyObject *) PyMemoryView_FromBuffer(const Py_buffer *info);
 #endif
 PyAPI_FUNC(PyObject *) PyMemoryView_GetContiguous(PyObject *base,
                                                   int buffertype,
diff --git a/Misc/NEWS.d/next/C API/2021-12-21-22-56-36.bpo-46140.dvXkYK.rst b/Misc/NEWS.d/next/C API/2021-12-21-22-56-36.bpo-46140.dvXkYK.rst
new file mode 100644
index 0000000000000..26b985924b54d
--- /dev/null
+++ b/Misc/NEWS.d/next/C API/2021-12-21-22-56-36.bpo-46140.dvXkYK.rst	
@@ -0,0 +1 @@
+:c:func:`PyBuffer_GetPointer`, :c:func:`PyBuffer_FromContiguous`, :c:func:`PyBuffer_ToContiguous` and :c:func:`PyMemoryView_FromBuffer` now take buffer info by ``const Py_buffer *`` instead of ``Py_buffer *``, as they do not need mutability. :c:func:`PyBuffer_FromContiguous` also now takes the source buffer as ``const void *``, and similarly :c:func:`PyBuffer_GetPointer` takes the strides as ``const Py_ssize_t *``.
\ No newline at end of file
diff --git a/Objects/abstract.c b/Objects/abstract.c
index 5c533bf03292d..6a2d5eda14079 100644
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -474,7 +474,7 @@ PyBuffer_IsContiguous(const Py_buffer *view, char order)
 
 
 void*
-PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices)
+PyBuffer_GetPointer(const Py_buffer *view, const Py_ssize_t *indices)
 {
     char* pointer;
     int i;
@@ -564,12 +564,13 @@ PyBuffer_SizeFromFormat(const char *format)
 }
 
 int
-PyBuffer_FromContiguous(Py_buffer *view, void *buf, Py_ssize_t len, char fort)
+PyBuffer_FromContiguous(const Py_buffer *view, const void *buf, Py_ssize_t len, char fort)
 {
     int k;
     void (*addone)(int, Py_ssize_t *, const Py_ssize_t *);
     Py_ssize_t *indices, elements;
-    char *src, *ptr;
+    char *ptr;
+    const char *src;
 
     if (len > view->len) {
         len = view->len;
diff --git a/Objects/memoryobject.c b/Objects/memoryobject.c
index 6257455d34715..dfcb31e5b3307 100644
--- a/Objects/memoryobject.c
+++ b/Objects/memoryobject.c
@@ -389,7 +389,7 @@ copy_rec(const Py_ssize_t *shape, Py_ssize_t ndim, Py_ssize_t itemsize,
 
 /* Faster copying of one-dimensional arrays. */
 static int
-copy_single(Py_buffer *dest, Py_buffer *src)
+copy_single(const Py_buffer *dest, const Py_buffer *src)
 {
     char *mem = NULL;
 
@@ -421,7 +421,7 @@ copy_single(Py_buffer *dest, Py_buffer *src)
    structure. Copying is atomic, the function never fails with a partial
    copy. */
 static int
-copy_buffer(Py_buffer *dest, Py_buffer *src)
+copy_buffer(const Py_buffer *dest, const Py_buffer *src)
 {
     char *mem = NULL;
 
@@ -479,7 +479,7 @@ init_fortran_strides_from_shape(Py_buffer *view)
    or 'A' (Any). Assumptions: src has PyBUF_FULL information, src->ndim >= 1,
    len(mem) == src->len. */
 static int
-buffer_to_contiguous(char *mem, Py_buffer *src, char order)
+buffer_to_contiguous(char *mem, const Py_buffer *src, char order)
 {
     Py_buffer dest;
     Py_ssize_t *strides;
@@ -755,7 +755,7 @@ PyMemoryView_FromMemory(char *mem, Py_ssize_t size, int flags)
    without full information. Because of this fact init_shape_strides()
    must be able to reconstruct missing values.  */
 PyObject *
-PyMemoryView_FromBuffer(Py_buffer *info)
+PyMemoryView_FromBuffer(const Py_buffer *info)
 {
     _PyManagedBufferObject *mbuf;
     PyObject *mv;
@@ -840,7 +840,7 @@ mbuf_copy_format(_PyManagedBufferObject *mbuf, const char *fmt)
         passes the altered format pointer to PyBuffer_Release().
 */
 static PyObject *
-memory_from_contiguous_copy(Py_buffer *src, char order)
+memory_from_contiguous_copy(const Py_buffer *src, char order)
 {
     _PyManagedBufferObject *mbuf;
     PyMemoryViewObject *mv;
@@ -982,7 +982,7 @@ typedef struct {
 } Py_buffer_full;
 
 int
-PyBuffer_ToContiguous(void *buf, Py_buffer *src, Py_ssize_t len, char order)
+PyBuffer_ToContiguous(void *buf, const Py_buffer *src, Py_ssize_t len, char order)
 {
     Py_buffer_full *fb = NULL;
     int ret;
@@ -2271,7 +2271,7 @@ memory_repr(PyMemoryViewObject *self)
 /**************************************************************************/
 
 static char *
-lookup_dimension(Py_buffer *view, char *ptr, int dim, Py_ssize_t index)
+lookup_dimension(const Py_buffer *view, char *ptr, int dim, Py_ssize_t index)
 {
     Py_ssize_t nitems; /* items in the given dimension */
 
@@ -2297,7 +2297,7 @@ lookup_dimension(Py_buffer *view, char *ptr, int dim, Py_ssize_t index)
 
 /* Get the pointer to the item at index. */
 static char *
-ptr_from_index(Py_buffer *view, Py_ssize_t index)
+ptr_from_index(const Py_buffer *view, Py_ssize_t index)
 {
     char *ptr = (char *)view->buf;
     return lookup_dimension(view, ptr, 0, index);
@@ -2305,7 +2305,7 @@ ptr_from_index(Py_buffer *view, Py_ssize_t index)
 
 /* Get the pointer to the item at tuple. */
 static char *
-ptr_from_tuple(Py_buffer *view, PyObject *tup)
+ptr_from_tuple(const Py_buffer *view, PyObject *tup)
 {
     char *ptr = (char *)view->buf;
     Py_ssize_t dim, nindices = PyTuple_GET_SIZE(tup);



More information about the Python-checkins mailing list