Kaydet (Commit) 06e1ab0a authored tarafından Nick Coghlan's avatar Nick Coghlan

Close #15573: use value-based memoryview comparisons (patch by Stefan Krah)

üst 5c0b1ca5
...@@ -2405,6 +2405,52 @@ copying. ...@@ -2405,6 +2405,52 @@ copying.
:class:`memoryview` has several methods: :class:`memoryview` has several methods:
.. method:: __eq__(exporter)
A memoryview and a :pep:`3118` exporter are equal if their shapes are
equivalent and if all corresponding values are equal when the operands'
respective format codes are interpreted using :mod:`struct` syntax.
For the subset of :mod:`struct` format strings currently supported by
:meth:`tolist`, ``v`` and ``w`` are equal if ``v.tolist() == w.tolist()``::
>>> import array
>>> a = array.array('I', [1, 2, 3, 4, 5])
>>> b = array.array('d', [1.0, 2.0, 3.0, 4.0, 5.0])
>>> c = array.array('b', [5, 3, 1])
>>> x = memoryview(a)
>>> y = memoryview(b)
>>> x == a == y == b
True
>>> x.tolist() == a.tolist() == y.tolist() == b.tolist()
True
>>> z = y[::-2]
>>> z == c
True
>>> z.tolist() == c.tolist()
True
If either format string is not supported by the :mod:`struct` module,
then the objects will always compare as unequal (even if the format
strings and buffer contents are identical)::
>>> from ctypes import BigEndianStructure, c_long
>>> class BEPoint(BigEndianStructure):
... _fields_ = [("x", c_long), ("y", c_long)]
...
>>> point = BEPoint(100, 200)
>>> a = memoryview(point)
>>> b = memoryview(point)
>>> a == point
False
>>> a == b
False
Note that, as with floating point numbers, ``v is w`` does *not* imply
``v == w`` for memoryview objects.
.. versionchanged:: 3.3
.. method:: tobytes() .. method:: tobytes()
Return the data in the buffer as a bytestring. This is equivalent to Return the data in the buffer as a bytestring. This is equivalent to
...@@ -2417,7 +2463,9 @@ copying. ...@@ -2417,7 +2463,9 @@ copying.
b'abc' b'abc'
For non-contiguous arrays the result is equal to the flattened list For non-contiguous arrays the result is equal to the flattened list
representation with all elements converted to bytes. representation with all elements converted to bytes. :meth:`tobytes`
supports all format strings, including those that are not in
:mod:`struct` module syntax.
.. method:: tolist() .. method:: tolist()
...@@ -2431,6 +2479,9 @@ copying. ...@@ -2431,6 +2479,9 @@ copying.
>>> m.tolist() >>> m.tolist()
[1.1, 2.2, 3.3] [1.1, 2.2, 3.3]
:meth:`tolist` is currently restricted to single character native formats
in :mod:`struct` module syntax.
.. method:: release() .. method:: release()
Release the underlying buffer exposed by the memoryview object. Many Release the underlying buffer exposed by the memoryview object. Many
...@@ -2470,7 +2521,10 @@ copying. ...@@ -2470,7 +2521,10 @@ copying.
``[byte_length//new_itemsize]``, which means that the result view ``[byte_length//new_itemsize]``, which means that the result view
will be one-dimensional. The return value is a new memoryview, but will be one-dimensional. The return value is a new memoryview, but
the buffer itself is not copied. Supported casts are 1D -> C-contiguous the buffer itself is not copied. Supported casts are 1D -> C-contiguous
and C-contiguous -> 1D. One of the formats must be a byte format and C-contiguous -> 1D.
Both formats are restricted to single element native formats in
:mod:`struct` syntax. One of the formats must be a byte format
('B', 'b' or 'c'). The byte length of the result must be the same ('B', 'b' or 'c'). The byte length of the result must be the same
as the original length. as the original length.
...@@ -2608,25 +2662,7 @@ copying. ...@@ -2608,25 +2662,7 @@ copying.
A string containing the format (in :mod:`struct` module style) for each A string containing the format (in :mod:`struct` module style) for each
element in the view. A memoryview can be created from exporters with element in the view. A memoryview can be created from exporters with
arbitrary format strings, but some methods (e.g. :meth:`tolist`) are arbitrary format strings, but some methods (e.g. :meth:`tolist`) are
restricted to native single element formats. Special care must be taken restricted to native single element formats.
when comparing memoryviews. Since comparisons are required to return a
value for ``==`` and ``!=``, two memoryviews referencing the same
exporter can compare as not-equal if the exporter's format is not
understood::
>>> from ctypes import BigEndianStructure, c_long
>>> class BEPoint(BigEndianStructure):
... _fields_ = [("x", c_long), ("y", c_long)]
...
>>> point = BEPoint(100, 200)
>>> a = memoryview(point)
>>> b = memoryview(point)
>>> a == b
False
>>> a.tolist()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NotImplementedError: memoryview: unsupported format T{>l:x:>l:y:}
.. attribute:: itemsize .. attribute:: itemsize
......
...@@ -162,7 +162,6 @@ Features ...@@ -162,7 +162,6 @@ Features
and the view is read-only. (Contributed by Antoine Pitrou in and the view is read-only. (Contributed by Antoine Pitrou in
:issue:`13411`) :issue:`13411`)
* Arbitrary slicing of any 1-D arrays type is supported. For example, it * Arbitrary slicing of any 1-D arrays type is supported. For example, it
is now possible to reverse a memoryview in O(1) by using a negative step. is now possible to reverse a memoryview in O(1) by using a negative step.
...@@ -178,6 +177,12 @@ API changes ...@@ -178,6 +177,12 @@ API changes
now returns an integer (in accordance with the struct module syntax). now returns an integer (in accordance with the struct module syntax).
For returning a bytes object the view must be cast to 'c' first. For returning a bytes object the view must be cast to 'c' first.
* memoryview comparisons now use the logical structure of the operands
and compare all array elements by value. All format strings in struct
module syntax are supported. Views with unrecognised format strings
are still permitted, but will always compare as unequal, regardless
of view contents.
* For further changes see `Build and C API Changes`_ and `Porting C code`_ . * For further changes see `Build and C API Changes`_ and `Porting C code`_ .
.. _pep-393: .. _pep-393:
......
This diff is collapsed.
...@@ -10,6 +10,9 @@ What's New in Python 3.3.0 Release Candidate 1? ...@@ -10,6 +10,9 @@ What's New in Python 3.3.0 Release Candidate 1?
Core and Builtins Core and Builtins
----------------- -----------------
- Issue #15573: memoryview comparisons are now performed by value with full
support for any valid struct module format definition.
- Issue #15316: When an item in the fromlist for __import__ doesn't exist, - Issue #15316: When an item in the fromlist for __import__ doesn't exist,
don't raise an error, but if an exception is raised as part of an import do don't raise an error, but if an exception is raised as part of an import do
let that propagate. let that propagate.
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment