Skip to content

Commit 5124891

Browse files
committed
Added public PyObject_CopyToObject() to pybuffer.h
1 parent 782217f commit 5124891

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

Include/pybuffer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ PyAPI_FUNC(int) PyBuffer_FromContiguous(const Py_buffer *view, const void *buf,
7575
is 'A', then it does not matter and the copy will be made
7676
in whatever way is more efficient. */
7777
PyAPI_FUNC(int) PyObject_CopyData(PyObject *dest, PyObject *src);
78+
PyAPI_FUNC(int) PyObject_CopyToObject(PyObject *obj, void *buf,
79+
Py_ssize_t len, char fortran);
7880

7981
/* Copy the data from the src buffer to the buffer of destination. */
8082
PyAPI_FUNC(int) PyBuffer_IsContiguous(const Py_buffer *view, char fort);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added public :c:func:`PyObject_CopyToObject()` to ``pybuffer.h``

Objects/abstract.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,40 @@ int PyObject_CopyData(PyObject *dest, PyObject *src)
733733
return 0;
734734
}
735735

736+
int
737+
PyObject_CopyToObject(PyObject *obj, void *buf, Py_ssize_t len, char fort)
738+
{
739+
Py_buffer view_obj;
740+
741+
if (!PyObject_CheckBuffer(obj)) {
742+
PyErr_SetString(PyExc_TypeError,
743+
"object doesn't support buffer");
744+
return -1;
745+
}
746+
747+
if (PyObject_GetBuffer(obj, &view_obj, PyBUF_SIMPLE)) {
748+
return -1;
749+
}
750+
751+
if (view_obj.len < len) {
752+
PyErr_SetString(PyExc_BufferError,
753+
"buffer length is insufficient");
754+
PyBuffer_Release(&view_obj);
755+
return -1;
756+
}
757+
758+
/* just copy it directly through memcpy */
759+
if (fort == 'C' || fort == 'F') {
760+
memcpy(view_obj.buf, buf, len);
761+
PyBuffer_Release(&view_obj);
762+
return 0;
763+
}
764+
765+
/* i'm thinking... */
766+
767+
return -1;
768+
}
769+
736770
void
737771
PyBuffer_FillContiguousStrides(int nd, Py_ssize_t *shape,
738772
Py_ssize_t *strides, int itemsize,

0 commit comments

Comments
 (0)