diff options
Diffstat (limited to 'Objects/memoryobject.c')
-rw-r--r-- | Objects/memoryobject.c | 44 |
1 files changed, 35 insertions, 9 deletions
diff --git a/Objects/memoryobject.c b/Objects/memoryobject.c index 40e6308..d835704 100644 --- a/Objects/memoryobject.c +++ b/Objects/memoryobject.c @@ -2120,22 +2120,39 @@ memory_tolist(PyMemoryViewObject *mv, PyObject *noargs) } static PyObject * -memory_tobytes(PyMemoryViewObject *self, PyObject *dummy) +memory_tobytes(PyMemoryViewObject *self, PyObject *args, PyObject *kwds) { + static char *kwlist[] = {"order", NULL}; Py_buffer *src = VIEW_ADDR(self); - PyObject *bytes = NULL; + char *order = NULL; + char ord = 'C'; + PyObject *bytes; CHECK_RELEASED(self); - if (MV_C_CONTIGUOUS(self->flags)) { - return PyBytes_FromStringAndSize(src->buf, src->len); + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|z", kwlist, &order)) { + return NULL; + } + + if (order) { + if (strcmp(order, "F") == 0) { + ord = 'F'; + } + else if (strcmp(order, "A") == 0) { + ord = 'A'; + } + else if (strcmp(order, "C") != 0) { + PyErr_SetString(PyExc_ValueError, + "order must be 'C', 'F' or 'A'"); + return NULL; + } } bytes = PyBytes_FromStringAndSize(NULL, src->len); if (bytes == NULL) return NULL; - if (buffer_to_contiguous(PyBytes_AS_STRING(bytes), src, 'C') < 0) { + if (PyBuffer_ToContiguous(PyBytes_AS_STRING(bytes), src, src->len, ord) < 0) { Py_DECREF(bytes); return NULL; } @@ -2156,10 +2173,15 @@ memory_hex(PyMemoryViewObject *self, PyObject *dummy) return _Py_strhex(src->buf, src->len); } - bytes = memory_tobytes(self, dummy); + bytes = PyBytes_FromStringAndSize(NULL, src->len); if (bytes == NULL) return NULL; + if (PyBuffer_ToContiguous(PyBytes_AS_STRING(bytes), src, src->len, 'C') < 0) { + Py_DECREF(bytes); + return NULL; + } + ret = _Py_strhex(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes)); Py_DECREF(bytes); @@ -3061,9 +3083,13 @@ PyDoc_STRVAR(memory_release_doc, \n\ Release the underlying buffer exposed by the memoryview object."); PyDoc_STRVAR(memory_tobytes_doc, -"tobytes($self, /)\n--\n\ +"tobytes($self, /, order=None)\n--\n\ \n\ -Return the data in the buffer as a byte string."); +Return the data in the buffer as a byte string. Order can be {'C', 'F', 'A'}.\n\ +When order is 'C' or 'F', the data of the original array is converted to C or\n\ +Fortran order. For contiguous views, 'A' returns an exact copy of the physical\n\ +memory. In particular, in-memory Fortran order is preserved. For non-contiguous\n\ +views, the data is converted to C first. order=None is the same as order='C'."); PyDoc_STRVAR(memory_hex_doc, "hex($self, /)\n--\n\ \n\ @@ -3083,7 +3109,7 @@ Return a readonly version of the memoryview."); static PyMethodDef memory_methods[] = { {"release", (PyCFunction)memory_release, METH_NOARGS, memory_release_doc}, - {"tobytes", (PyCFunction)memory_tobytes, METH_NOARGS, memory_tobytes_doc}, + {"tobytes", (PyCFunction)memory_tobytes, METH_VARARGS|METH_KEYWORDS, memory_tobytes_doc}, {"hex", (PyCFunction)memory_hex, METH_NOARGS, memory_hex_doc}, {"tolist", (PyCFunction)memory_tolist, METH_NOARGS, memory_tolist_doc}, {"cast", (PyCFunction)(void(*)(void))memory_cast, METH_VARARGS|METH_KEYWORDS, memory_cast_doc}, |