• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.. highlight:: c
2
3.. _memoryview-objects:
4
5.. index::
6   object: memoryview
7
8MemoryView objects
9------------------
10
11A :class:`memoryview` object exposes the C level :ref:`buffer interface
12<bufferobjects>` as a Python object which can then be passed around like
13any other object.
14
15
16.. c:function:: PyObject *PyMemoryView_FromObject(PyObject *obj)
17
18   Create a memoryview object from an object that provides the buffer interface.
19   If *obj* supports writable buffer exports, the memoryview object will be
20   read/write, otherwise it may be either read-only or read/write at the
21   discretion of the exporter.
22
23.. c:function:: PyObject *PyMemoryView_FromMemory(char *mem, Py_ssize_t size, int flags)
24
25   Create a memoryview object using *mem* as the underlying buffer.
26   *flags* can be one of :c:macro:`PyBUF_READ` or :c:macro:`PyBUF_WRITE`.
27
28   .. versionadded:: 3.3
29
30.. c:function:: PyObject *PyMemoryView_FromBuffer(Py_buffer *view)
31
32   Create a memoryview object wrapping the given buffer structure *view*.
33   For simple byte buffers, :c:func:`PyMemoryView_FromMemory` is the preferred
34   function.
35
36.. c:function:: PyObject *PyMemoryView_GetContiguous(PyObject *obj, int buffertype, char order)
37
38   Create a memoryview object to a :term:`contiguous` chunk of memory (in either
39   'C' or 'F'ortran *order*) from an object that defines the buffer
40   interface. If memory is contiguous, the memoryview object points to the
41   original memory. Otherwise, a copy is made and the memoryview points to a
42   new bytes object.
43
44
45.. c:function:: int PyMemoryView_Check(PyObject *obj)
46
47   Return true if the object *obj* is a memoryview object.  It is not
48   currently allowed to create subclasses of :class:`memoryview`.  This
49   function always succeeds.
50
51
52.. c:function:: Py_buffer *PyMemoryView_GET_BUFFER(PyObject *mview)
53
54   Return a pointer to the memoryview's private copy of the exporter's buffer.
55   *mview* **must** be a memoryview instance; this macro doesn't check its type,
56   you must do it yourself or you will risk crashes.
57
58.. c:function:: Py_buffer *PyMemoryView_GET_BASE(PyObject *mview)
59
60   Return either a pointer to the exporting object that the memoryview is based
61   on or ``NULL`` if the memoryview has been created by one of the functions
62   :c:func:`PyMemoryView_FromMemory` or :c:func:`PyMemoryView_FromBuffer`.
63   *mview* **must** be a memoryview instance.
64
65