# HG changeset patch # Parent a2df4baa112ba329f4a05c47812df22f40068ea2 diff -r a2df4baa112b Doc/c-api/buffer.rst --- a/Doc/c-api/buffer.rst Wed Apr 01 13:06:18 2015 +0300 +++ b/Doc/c-api/buffer.rst Wed Apr 01 10:09:40 2015 +0000 @@ -284,6 +284,16 @@ contiguity requests ~~~~~~~~~~~~~~~~~~~ +For the purposes of the buffer protocol, a buffer is considered contiguous if +its elements are contiguous in memory, and its memory layout is like a C or +Fortran array. C and Fortran layout differs when more than one dimension is +involved. Consider a two-dimensional array with ``shape = {2, 3}``. Its +C-contiguous layout would be as 2 one-dimensional sub-arrays, each of 3 +elements, while its Fortran-contiguous layout would consider the indexes in +the opposite order: 3 sub-arrays, each of 2 elements. Zero-dimensional +buffers (with a single element) are considered both C- and +Fortran-contiguous. + C or Fortran contiguity can be explicitly requested, with and without stride information. Without stride information, the buffer must be C-contiguous. diff -r a2df4baa112b Doc/glossary.rst --- a/Doc/glossary.rst Wed Apr 01 13:06:18 2015 +0300 +++ b/Doc/glossary.rst Wed Apr 01 10:09:40 2015 +0000 @@ -86,7 +86,8 @@ A :term:`text file` reads and writes :class:`str` objects. bytes-like object - An object that supports the :ref:`bufferobjects`, like :class:`bytes`, + An object that supports the :ref:`bufferobjects` + and is C-contiguous, like :class:`bytes`, :class:`bytearray` or :class:`memoryview`. Bytes-like objects can be used for various operations that expect binary data, such as compression, saving to a binary file or sending over a socket. diff -r a2df4baa112b Doc/library/stdtypes.rst --- a/Doc/library/stdtypes.rst Wed Apr 01 13:06:18 2015 +0300 +++ b/Doc/library/stdtypes.rst Wed Apr 01 10:09:40 2015 +0000 @@ -3307,11 +3307,12 @@ is a single byte, but other types such as :class:`array.array` may have bigger elements. - ``len(view)`` is equal to the length of :class:`~memoryview.tolist`. - If ``view.ndim = 0``, the length is 1. If ``view.ndim = 1``, the length + If ``view.ndim == 0``, then ``len(view)`` is 1, otherwise, + ``len(view)`` is equal to the flattened length of :meth:`tolist`. + If ``view.ndim == 1``, the length is equal to the number of elements in the view. For higher dimensions, the length is equal to the length of the nested list representation of - the view. The :class:`~memoryview.itemsize` attribute will give you the + the view. The :attr:`itemsize` attribute will give the number of bytes in a single element. A :class:`memoryview` supports slicing and indexing to expose its data. @@ -3444,8 +3445,9 @@ .. method:: tobytes() - Return the data in the buffer as a bytestring. This is equivalent to - calling the :class:`bytes` constructor on the memoryview. :: + Return a copy of the data in the buffer as a bytestring. + This is equivalent to calling the :class:`bytes` constructor on the + memoryview. :: >>> m = memoryview(b"abc") >>> m.tobytes() @@ -3453,8 +3455,12 @@ >>> bytes(m) b'abc' - For non-contiguous arrays the result is equal to the flattened list - representation with all elements converted to bytes. :meth:`tobytes` + The result is equal to the concatenated series of elements, with + all elements converted to bytes. For multi-dimensional arrays, the + order of the elements is the same as the flattened order of what + :meth:`tolist` would return, assuming it supports the element format. + If the view is not C-contiguous, the resulting bytes may be arranged + differently from the original memory. The :meth:`tobytes` method supports all format strings, including those that are not in :mod:`struct` module syntax. @@ -3701,19 +3707,22 @@ .. attribute:: c_contiguous - A bool indicating whether the memory is C-contiguous. + A bool indicating whether the elements are contiguous and layed out in + memory like a C array. .. versionadded:: 3.3 .. attribute:: f_contiguous - A bool indicating whether the memory is Fortran contiguous. + A bool indicating whether the elements are contiguous and layed out in + memory like a Fortran array. .. versionadded:: 3.3 .. attribute:: contiguous - A bool indicating whether the memory is contiguous. + A bool indicating whether the elements are contiguous and layed out in + memory like either a C or Fortran array. .. versionadded:: 3.3