Issue #2396: backport the memoryview object.
diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst
index c19462c..5ca6e42 100644
--- a/Doc/library/functions.rst
+++ b/Doc/library/functions.rst
@@ -680,6 +680,13 @@
Added support for the optional *key* argument.
+.. function:: memoryview(obj)
+ :noindex:
+
+ Return a "memory view" object created from the given argument. See
+ :ref:`typememoryview` for more information.
+
+
.. function:: min(iterable[, args...][key])
With a single argument *iterable*, return the smallest item of a non-empty
diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst
index 03565b0..af84f4f 100644
--- a/Doc/library/stdtypes.rst
+++ b/Doc/library/stdtypes.rst
@@ -2354,6 +2354,104 @@
state.
+.. _typememoryview:
+
+memoryview Types
+================
+
+:class:`memoryview`\s allow Python code to access the internal data of an object
+that supports the buffer protocol without copying. Memory can be interpreted as
+simple bytes or complex data structures.
+
+.. class:: memoryview(obj)
+
+ Create a :class:`memoryview` that references *obj*. *obj* must support the
+ buffer protocol. Builtin objects that support the buffer protocol include
+ :class:`str` and :class:`bytearray` (but not :class:`unicode`).
+
+ ``len(view)`` returns the total number of bytes in the memoryview, *view*.
+
+ A :class:`memoryview` supports slicing to expose its data. Taking a single
+ index will return a single byte. Full slicing will result in a subview::
+
+ >>> v = memoryview('abcefg')
+ >>> v[1]
+ 'b'
+ >>> v[-1]
+ 'g'
+ >>> v[1:4]
+ <memory at 0x77ab28>
+ >>> str(v[1:4])
+ 'bce'
+ >>> v[3:-1]
+ <memory at 0x744f18>
+ >>> str(v[4:-1])
+ 'f'
+
+ If the object the memory view is over supports changing its data, the
+ memoryview supports slice assignment::
+
+ >>> data = bytearray('abcefg')
+ >>> v = memoryview(data)
+ >>> v.readonly
+ False
+ >>> v[0] = 'z'
+ >>> data
+ bytearray(b'zbcefg')
+ >>> v[1:4] = '123'
+ >>> data
+ bytearray(b'z123fg')
+ >>> v[2] = 'spam'
+ Traceback (most recent call last):
+ File "<stdin>", line 1, in <module>
+ ValueError: cannot modify size of memoryview object
+
+ Notice how the size of the memoryview object can not be changed.
+
+
+ :class:`memoryview` has two methods:
+
+ .. method:: tobytes()
+
+ Return the data in the buffer as a bytestring (an object of class
+ :class:`str`).
+
+ .. method:: tolist()
+
+ Return the data in the buffer as a list of integers. ::
+
+ >>> memoryview(b'abc').tolist()
+ [97, 98, 99]
+
+ There are also several readonly attributes available:
+
+ .. attribute:: format
+
+ A string containing the format (in :mod:`struct` module style) for each
+ element in the view. This defaults to ``'B'``, a simple bytestring.
+
+ .. attribute:: itemsize
+
+ The size in bytes of each element of the memoryview.
+
+ .. attribute:: shape
+
+ A tuple of integers the length of :attr:`ndim` giving the shape of the
+ memory as a N-dimensional array.
+
+ .. attribute:: ndim
+
+ An integer indicating how many dimensions of a multi-dimensional array the
+ memory represents.
+
+ .. attribute:: strides
+
+ A tuple of integers the length of :attr:`ndim` giving the size in bytes to
+ access each element for each dimension of the array.
+
+ .. memoryview.suboffsets isn't documented because it only seems useful for C
+
+
.. _typecontextmanager:
Context Manager Types