Skip to content

Commit 31ff967

Browse files
authored
bpo-46140: take more Py_buffer arguments as const * (GH-30217)
1 parent fc54e72 commit 31ff967

File tree

7 files changed

+23
-22
lines changed

7 files changed

+23
-22
lines changed

Doc/c-api/buffer.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -470,27 +470,27 @@ Buffer-related functions
470470
.. versionadded:: 3.9
471471
472472
473-
.. c:function:: int PyBuffer_IsContiguous(Py_buffer *view, char order)
473+
.. c:function:: int PyBuffer_IsContiguous(const Py_buffer *view, char order)
474474
475475
Return ``1`` if the memory defined by the *view* is C-style (*order* is
476476
``'C'``) or Fortran-style (*order* is ``'F'``) :term:`contiguous` or either one
477477
(*order* is ``'A'``). Return ``0`` otherwise. This function always succeeds.
478478
479479
480-
.. c:function:: void* PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices)
480+
.. c:function:: void* PyBuffer_GetPointer(const Py_buffer *view, const Py_ssize_t *indices)
481481
482482
Get the memory area pointed to by the *indices* inside the given *view*.
483483
*indices* must point to an array of ``view->ndim`` indices.
484484
485485
486-
.. c:function:: int PyBuffer_FromContiguous(Py_buffer *view, void *buf, Py_ssize_t len, char fort)
486+
.. c:function:: int PyBuffer_FromContiguous(const Py_buffer *view, const void *buf, Py_ssize_t len, char fort)
487487
488488
Copy contiguous *len* bytes from *buf* to *view*.
489489
*fort* can be ``'C'`` or ``'F'`` (for C-style or Fortran-style ordering).
490490
``0`` is returned on success, ``-1`` on error.
491491
492492
493-
.. c:function:: int PyBuffer_ToContiguous(void *buf, Py_buffer *src, Py_ssize_t len, char order)
493+
.. c:function:: int PyBuffer_ToContiguous(void *buf, const Py_buffer *src, Py_ssize_t len, char order)
494494
495495
Copy *len* bytes from *src* to its contiguous representation in *buf*.
496496
*order* can be ``'C'`` or ``'F'`` or ``'A'`` (for C-style or Fortran-style

Doc/c-api/memoryview.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ any other object.
2727
2828
.. versionadded:: 3.3
2929
30-
.. c:function:: PyObject *PyMemoryView_FromBuffer(Py_buffer *view)
30+
.. c:function:: PyObject *PyMemoryView_FromBuffer(const Py_buffer *view)
3131
3232
Create a memoryview object wrapping the given buffer structure *view*.
3333
For simple byte buffers, :c:func:`PyMemoryView_FromMemory` is the preferred
@@ -61,4 +61,3 @@ any other object.
6161
on or ``NULL`` if the memoryview has been created by one of the functions
6262
:c:func:`PyMemoryView_FromMemory` or :c:func:`PyMemoryView_FromBuffer`.
6363
*mview* **must** be a memoryview instance.
64-

Include/cpython/abstract.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,17 +183,17 @@ PyAPI_FUNC(int) PyObject_GetBuffer(PyObject *obj, Py_buffer *view,
183183

184184
/* Get the memory area pointed to by the indices for the buffer given.
185185
Note that view->ndim is the assumed size of indices. */
186-
PyAPI_FUNC(void *) PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices);
186+
PyAPI_FUNC(void *) PyBuffer_GetPointer(const Py_buffer *view, const Py_ssize_t *indices);
187187

188188
/* Return the implied itemsize of the data-format area from a
189189
struct-style description. */
190190
PyAPI_FUNC(Py_ssize_t) PyBuffer_SizeFromFormat(const char *format);
191191

192192
/* Implementation in memoryobject.c */
193-
PyAPI_FUNC(int) PyBuffer_ToContiguous(void *buf, Py_buffer *view,
193+
PyAPI_FUNC(int) PyBuffer_ToContiguous(void *buf, const Py_buffer *view,
194194
Py_ssize_t len, char order);
195195

196-
PyAPI_FUNC(int) PyBuffer_FromContiguous(Py_buffer *view, void *buf,
196+
PyAPI_FUNC(int) PyBuffer_FromContiguous(const Py_buffer *view, const void *buf,
197197
Py_ssize_t len, char order);
198198

199199
/* Copy len bytes of data from the contiguous chunk of memory

Include/memoryobject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ PyAPI_FUNC(PyObject *) PyMemoryView_FromMemory(char *mem, Py_ssize_t size,
2626
int flags);
2727
#endif
2828
#ifndef Py_LIMITED_API
29-
PyAPI_FUNC(PyObject *) PyMemoryView_FromBuffer(Py_buffer *info);
29+
PyAPI_FUNC(PyObject *) PyMemoryView_FromBuffer(const Py_buffer *info);
3030
#endif
3131
PyAPI_FUNC(PyObject *) PyMemoryView_GetContiguous(PyObject *base,
3232
int buffertype,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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 *``.

Objects/abstract.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ PyBuffer_IsContiguous(const Py_buffer *view, char order)
474474

475475

476476
void*
477-
PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices)
477+
PyBuffer_GetPointer(const Py_buffer *view, const Py_ssize_t *indices)
478478
{
479479
char* pointer;
480480
int i;
@@ -564,12 +564,13 @@ PyBuffer_SizeFromFormat(const char *format)
564564
}
565565

566566
int
567-
PyBuffer_FromContiguous(Py_buffer *view, void *buf, Py_ssize_t len, char fort)
567+
PyBuffer_FromContiguous(const Py_buffer *view, const void *buf, Py_ssize_t len, char fort)
568568
{
569569
int k;
570570
void (*addone)(int, Py_ssize_t *, const Py_ssize_t *);
571571
Py_ssize_t *indices, elements;
572-
char *src, *ptr;
572+
char *ptr;
573+
const char *src;
573574

574575
if (len > view->len) {
575576
len = view->len;

Objects/memoryobject.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ copy_rec(const Py_ssize_t *shape, Py_ssize_t ndim, Py_ssize_t itemsize,
389389

390390
/* Faster copying of one-dimensional arrays. */
391391
static int
392-
copy_single(Py_buffer *dest, Py_buffer *src)
392+
copy_single(const Py_buffer *dest, const Py_buffer *src)
393393
{
394394
char *mem = NULL;
395395

@@ -421,7 +421,7 @@ copy_single(Py_buffer *dest, Py_buffer *src)
421421
structure. Copying is atomic, the function never fails with a partial
422422
copy. */
423423
static int
424-
copy_buffer(Py_buffer *dest, Py_buffer *src)
424+
copy_buffer(const Py_buffer *dest, const Py_buffer *src)
425425
{
426426
char *mem = NULL;
427427

@@ -479,7 +479,7 @@ init_fortran_strides_from_shape(Py_buffer *view)
479479
or 'A' (Any). Assumptions: src has PyBUF_FULL information, src->ndim >= 1,
480480
len(mem) == src->len. */
481481
static int
482-
buffer_to_contiguous(char *mem, Py_buffer *src, char order)
482+
buffer_to_contiguous(char *mem, const Py_buffer *src, char order)
483483
{
484484
Py_buffer dest;
485485
Py_ssize_t *strides;
@@ -755,7 +755,7 @@ PyMemoryView_FromMemory(char *mem, Py_ssize_t size, int flags)
755755
without full information. Because of this fact init_shape_strides()
756756
must be able to reconstruct missing values. */
757757
PyObject *
758-
PyMemoryView_FromBuffer(Py_buffer *info)
758+
PyMemoryView_FromBuffer(const Py_buffer *info)
759759
{
760760
_PyManagedBufferObject *mbuf;
761761
PyObject *mv;
@@ -840,7 +840,7 @@ mbuf_copy_format(_PyManagedBufferObject *mbuf, const char *fmt)
840840
passes the altered format pointer to PyBuffer_Release().
841841
*/
842842
static PyObject *
843-
memory_from_contiguous_copy(Py_buffer *src, char order)
843+
memory_from_contiguous_copy(const Py_buffer *src, char order)
844844
{
845845
_PyManagedBufferObject *mbuf;
846846
PyMemoryViewObject *mv;
@@ -982,7 +982,7 @@ typedef struct {
982982
} Py_buffer_full;
983983

984984
int
985-
PyBuffer_ToContiguous(void *buf, Py_buffer *src, Py_ssize_t len, char order)
985+
PyBuffer_ToContiguous(void *buf, const Py_buffer *src, Py_ssize_t len, char order)
986986
{
987987
Py_buffer_full *fb = NULL;
988988
int ret;
@@ -2271,7 +2271,7 @@ memory_repr(PyMemoryViewObject *self)
22712271
/**************************************************************************/
22722272

22732273
static char *
2274-
lookup_dimension(Py_buffer *view, char *ptr, int dim, Py_ssize_t index)
2274+
lookup_dimension(const Py_buffer *view, char *ptr, int dim, Py_ssize_t index)
22752275
{
22762276
Py_ssize_t nitems; /* items in the given dimension */
22772277

@@ -2297,15 +2297,15 @@ lookup_dimension(Py_buffer *view, char *ptr, int dim, Py_ssize_t index)
22972297

22982298
/* Get the pointer to the item at index. */
22992299
static char *
2300-
ptr_from_index(Py_buffer *view, Py_ssize_t index)
2300+
ptr_from_index(const Py_buffer *view, Py_ssize_t index)
23012301
{
23022302
char *ptr = (char *)view->buf;
23032303
return lookup_dimension(view, ptr, 0, index);
23042304
}
23052305

23062306
/* Get the pointer to the item at tuple. */
23072307
static char *
2308-
ptr_from_tuple(Py_buffer *view, PyObject *tup)
2308+
ptr_from_tuple(const Py_buffer *view, PyObject *tup)
23092309
{
23102310
char *ptr = (char *)view->buf;
23112311
Py_ssize_t dim, nindices = PyTuple_GET_SIZE(tup);

0 commit comments

Comments
 (0)