Skip to content
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Doc/c-api/buffer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,15 @@ Buffer-related functions

``0`` is returned on success, ``-1`` on error.

.. c:function:: int PyObject_CopyToObject(PyObject *obj, void *buf, Py_ssize_t len, char fortran)

Copy data from *buf* to *obj* buffer. Can convert between C-style and
or Fortran-style buffers.

``0`` is returned on success, ``-1`` on error.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
``0`` is returned on success, ``-1`` on error.
Return ``0`` on success, or return ``-1`` on error.


.. versionadded:: 3.14

.. c:function:: void PyBuffer_FillContiguousStrides(int ndims, Py_ssize_t *shape, Py_ssize_t *strides, int itemsize, char order)

Fill the *strides* array with byte-strides of a :term:`contiguous` (C-style if
Expand Down
2 changes: 2 additions & 0 deletions Include/pybuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ PyAPI_FUNC(int) PyBuffer_FromContiguous(const Py_buffer *view, const void *buf,
is 'A', then it does not matter and the copy will be made
in whatever way is more efficient. */
PyAPI_FUNC(int) PyObject_CopyData(PyObject *dest, PyObject *src);
PyAPI_FUNC(int) PyObject_CopyToObject(PyObject *obj, void *buf,
Py_ssize_t len, char fort);

/* Copy the data from the src buffer to the buffer of destination. */
PyAPI_FUNC(int) PyBuffer_IsContiguous(const Py_buffer *view, char fort);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added public :c:func:`PyObject_CopyToObject()` to ``pybuffer.h``
38 changes: 37 additions & 1 deletion Objects/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,8 @@ PyBuffer_FromContiguous(const Py_buffer *view, const void *buf, Py_ssize_t len,
return 0;
}

int PyObject_CopyData(PyObject *dest, PyObject *src)
int
PyObject_CopyData(PyObject *dest, PyObject *src)
{
Py_buffer view_dest, view_src;
int k;
Expand Down Expand Up @@ -733,6 +734,41 @@ int PyObject_CopyData(PyObject *dest, PyObject *src)
return 0;
}

int
PyObject_CopyToObject(PyObject *obj, void *buf, Py_ssize_t len,
char Py_UNUSED(fort))
{
Py_buffer view_obj;

if (!PyObject_CheckBuffer(obj)) {
PyErr_SetString(PyExc_TypeError,
"destination must be bytes-like objects");
return -1;
}

if (!buf) {
PyErr_SetString(PyExc_MemoryError, "buf pointer is null");
return -1;
}

if (PyObject_GetBuffer(obj, &view_obj, PyBUF_SIMPLE)) {
return -1;
}

if (view_obj.len < len) {
PyErr_SetString(PyExc_BufferError,
"destination is too small to"
"receive data from buf");
PyBuffer_Release(&view_obj);
return -1;
}

/* just copy it directly through memcpy */
memcpy(view_obj.buf, buf, len);
PyBuffer_Release(&view_obj);
return 0;
}

void
PyBuffer_FillContiguousStrides(int nd, Py_ssize_t *shape,
Py_ssize_t *strides, int itemsize,
Expand Down
Loading