summaryrefslogtreecommitdiffstats
path: root/Objects/memoryobject.c
diff options
context:
space:
mode:
authorAntoine Pitrou <pitrou@free.fr>2018-04-14 17:49:21 (GMT)
committerGitHub <noreply@github.com>2018-04-14 17:49:21 (GMT)
commit480ab05d5fee2b8fa161f799af33086a4e68c7dd (patch)
treeca45c39193fb970c4d300b27346a9dcf8aaac814 /Objects/memoryobject.c
parentb1dc07509f78b354e83f5f4a902f1ff80c7bb05d (diff)
downloadcpython-480ab05d5fee2b8fa161f799af33086a4e68c7dd.zip
cpython-480ab05d5fee2b8fa161f799af33086a4e68c7dd.tar.gz
cpython-480ab05d5fee2b8fa161f799af33086a4e68c7dd.tar.bz2
bpo-33176: Add a toreadonly() method to memoryviews. (GH-6466)
Diffstat (limited to 'Objects/memoryobject.c')
-rw-r--r--Objects/memoryobject.c19
1 files changed, 19 insertions, 0 deletions
diff --git a/Objects/memoryobject.c b/Objects/memoryobject.c
index ccf45ff..adaa67c 100644
--- a/Objects/memoryobject.c
+++ b/Objects/memoryobject.c
@@ -1398,6 +1398,20 @@ error:
return NULL;
}
+static PyObject *
+memory_toreadonly(PyMemoryViewObject *self, PyObject *noargs)
+{
+ CHECK_RELEASED(self);
+ /* Even if self is already readonly, we still need to create a new
+ * object for .release() to work correctly.
+ */
+ self = (PyMemoryViewObject *) mbuf_add_view(self->mbuf, &self->view);
+ if (self != NULL) {
+ self->view.readonly = 1;
+ };
+ return (PyObject *) self;
+}
+
/**************************************************************************/
/* getbuffer */
@@ -3061,6 +3075,10 @@ PyDoc_STRVAR(memory_cast_doc,
"cast($self, /, format, *, shape)\n--\n\
\n\
Cast a memoryview to a new format or shape.");
+PyDoc_STRVAR(memory_toreadonly_doc,
+"toreadonly($self, /)\n--\n\
+\n\
+Return a readonly version of the memoryview.");
static PyMethodDef memory_methods[] = {
{"release", (PyCFunction)memory_release, METH_NOARGS, memory_release_doc},
@@ -3068,6 +3086,7 @@ static PyMethodDef memory_methods[] = {
{"hex", (PyCFunction)memory_hex, METH_NOARGS, memory_hex_doc},
{"tolist", (PyCFunction)memory_tolist, METH_NOARGS, memory_tolist_doc},
{"cast", (PyCFunction)memory_cast, METH_VARARGS|METH_KEYWORDS, memory_cast_doc},
+ {"toreadonly", (PyCFunction)memory_toreadonly, METH_NOARGS, memory_toreadonly_doc},
{"__enter__", memory_enter, METH_NOARGS, NULL},
{"__exit__", memory_exit, METH_VARARGS, NULL},
{NULL, NULL}