From: Alexander Belopolsky Date: Mon, 3 Sep 2012 20:43:55 +0000 (-0400) Subject: Issue #15855: added docstrings for memoryview methods and data descriptors (merge... X-Git-Tag: v3.3.1rc1~818^2^2~144 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e370c38131b3a5ebb434e17cff4e3362ddbb3f67;p=python Issue #15855: added docstrings for memoryview methods and data descriptors (merge 3.2). --- e370c38131b3a5ebb434e17cff4e3362ddbb3f67 diff --cc Objects/memoryobject.c index f547983a12,403aa68a9e..e0f20fe575 --- a/Objects/memoryobject.c +++ b/Objects/memoryobject.c @@@ -2841,51 -763,88 +2841,82 @@@ memory_ndim_get(PyMemoryViewObject *sel } static PyObject * -memory_richcompare(PyObject *v, PyObject *w, int op) +memory_c_contiguous(PyMemoryViewObject *self, PyObject *dummy) { - Py_buffer vv, ww; - int equal = 0; - PyObject *res; - - vv.obj = NULL; - ww.obj = NULL; - if (op != Py_EQ && op != Py_NE) - goto _notimpl; - if ((PyMemoryView_Check(v) && IS_RELEASED(v)) || - (PyMemoryView_Check(w) && IS_RELEASED(w))) { - equal = (v == w); - goto _end; - } - if (PyObject_GetBuffer(v, &vv, PyBUF_CONTIG_RO) == -1) { - PyErr_Clear(); - goto _notimpl; - } - if (PyObject_GetBuffer(w, &ww, PyBUF_CONTIG_RO) == -1) { - PyErr_Clear(); - goto _notimpl; - } - - if (vv.itemsize != ww.itemsize || vv.len != ww.len) - goto _end; - - equal = !memcmp(vv.buf, ww.buf, vv.len); - -_end: - PyBuffer_Release(&vv); - PyBuffer_Release(&ww); - if ((equal && op == Py_EQ) || (!equal && op == Py_NE)) - res = Py_True; - else - res = Py_False; - Py_INCREF(res); - return res; - -_notimpl: - PyBuffer_Release(&vv); - PyBuffer_Release(&ww); - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; + CHECK_RELEASED(self); + return PyBool_FromLong(MV_C_CONTIGUOUS(self->flags)); } - -static int -memory_traverse(PyMemoryViewObject *self, visitproc visit, void *arg) +static PyObject * +memory_f_contiguous(PyMemoryViewObject *self, PyObject *dummy) { - if (self->view.obj != NULL) - Py_VISIT(self->view.obj); - return 0; + CHECK_RELEASED(self); + return PyBool_FromLong(MV_F_CONTIGUOUS(self->flags)); } -static int -memory_clear(PyMemoryViewObject *self) +static PyObject * +memory_contiguous(PyMemoryViewObject *self, PyObject *dummy) { - PyBuffer_Release(&self->view); - return 0; + CHECK_RELEASED(self); + return PyBool_FromLong(MV_ANY_CONTIGUOUS(self->flags)); } ++PyDoc_STRVAR(memory_format_doc, ++ "A string containing the format (in struct module style)\n" ++ " for each element in the view."); ++PyDoc_STRVAR(memory_itemsize_doc, ++ "The size in bytes of each element of the memoryview."); ++PyDoc_STRVAR(memory_shape_doc, ++ "A tuple of ndim integers giving the shape of the memory\n" ++ " as an N-dimensional array."); ++PyDoc_STRVAR(memory_strides_doc, ++ "A tuple of ndim integers giving the size in bytes to access\n" ++ " each element for each dimension of the array."); ++PyDoc_STRVAR(memory_suboffsets_doc, ++ "A tuple of integers used internally for PIL-style arrays."); ++PyDoc_STRVAR(memory_readonly_doc, ++ "A bool indicating whether the memory is read only."); ++PyDoc_STRVAR(memory_ndim_doc, ++ "An integer indicating how many dimensions of a multi-dimensional\n" ++ " array the memory represents."); + -/* As mapping */ -static PyMappingMethods memory_as_mapping = { - (lenfunc)memory_length, /* mp_length */ - (binaryfunc)memory_subscript, /* mp_subscript */ - (objobjargproc)memory_ass_sub, /* mp_ass_subscript */ -}; - -static PySequenceMethods memory_as_sequence = { - 0, /* sq_length */ - 0, /* sq_concat */ - 0, /* sq_repeat */ - (ssizeargfunc)memory_item, /* sq_item */ +static PyGetSetDef memory_getsetlist[] = { + {"obj", (getter)memory_obj_get, NULL, NULL}, + {"nbytes", (getter)memory_nbytes_get, NULL, NULL}, - {"readonly", (getter)memory_readonly_get, NULL, NULL}, - {"itemsize", (getter)memory_itemsize_get, NULL, NULL}, - {"format", (getter)memory_format_get, NULL, NULL}, - {"ndim", (getter)memory_ndim_get, NULL, NULL}, - {"shape", (getter)memory_shape_get, NULL, NULL}, - {"strides", (getter)memory_strides_get, NULL, NULL}, - {"suboffsets", (getter)memory_suboffsets_get, NULL, NULL}, ++ {"readonly", (getter)memory_readonly_get, NULL, memory_readonly_doc}, ++ {"itemsize", (getter)memory_itemsize_get, NULL, memory_itemsize_doc}, ++ {"format", (getter)memory_format_get, NULL, memory_format_doc}, ++ {"ndim", (getter)memory_ndim_get, NULL, memory_ndim_doc}, ++ {"shape", (getter)memory_shape_get, NULL, memory_shape_doc}, ++ {"strides", (getter)memory_strides_get, NULL, memory_strides_doc}, ++ {"suboffsets", (getter)memory_suboffsets_get, NULL, memory_suboffsets_doc}, + {"c_contiguous", (getter)memory_c_contiguous, NULL, NULL}, + {"f_contiguous", (getter)memory_f_contiguous, NULL, NULL}, + {"contiguous", (getter)memory_contiguous, NULL, NULL}, + {NULL, NULL, NULL, NULL}, }; -/* Buffer methods */ ++PyDoc_STRVAR(memory_release_doc, ++"M.release() -> None\n\ ++\n\ ++Release the underlying buffer exposed by the memoryview object."); ++PyDoc_STRVAR(memory_tobytes_doc, ++"M.tobytes() -> bytes\n\ ++\n\ ++Return the data in the buffer as a byte string."); ++PyDoc_STRVAR(memory_tolist_doc, ++"M.tolist() -> list\n\ ++\n\ ++Return the data in the buffer as a list of elements."); -static PyBufferProcs memory_as_buffer = { - (getbufferproc)memory_getbuf, /* bf_getbuffer */ - (releasebufferproc)memory_releasebuf, /* bf_releasebuffer */ +static PyMethodDef memory_methods[] = { - {"release", (PyCFunction)memory_release, METH_NOARGS, NULL}, - {"tobytes", (PyCFunction)memory_tobytes, METH_NOARGS, NULL}, - {"tolist", (PyCFunction)memory_tolist, METH_NOARGS, NULL}, ++ {"release", (PyCFunction)memory_release, METH_NOARGS, memory_release_doc}, ++ {"tobytes", (PyCFunction)memory_tobytes, METH_NOARGS, memory_tobytes_doc}, ++ {"tolist", (PyCFunction)memory_tolist, METH_NOARGS, memory_tolist_doc}, + {"cast", (PyCFunction)memory_cast, METH_VARARGS|METH_KEYWORDS, NULL}, + {"__enter__", memory_enter, METH_NOARGS, NULL}, + {"__exit__", memory_exit, METH_VARARGS, NULL}, + {NULL, NULL} };