This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Fix iteration for memoryviews
Type: behavior Stage: patch review
Components: Interpreter Core Versions: Python 3.1, Python 2.7
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: r.david.murray, rhettinger
Priority: high Keywords: patch

Created on 2009-06-23 18:40 by rhettinger, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
mview.diff rhettinger, 2009-06-23 18:39 Code, test, and news entry.
mview2.diff rhettinger, 2009-06-23 19:34 Added a zero-dim guard
mview3.diff rhettinger, 2009-06-23 20:05 Nicely factored version
Messages (3)
msg89637 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2009-06-23 18:39
Despite being a sequence (with both __getitem__ and __len__ defined),
memoryview objects were not recognized as being iterable.  The docs say
that all such sequences are iterable, so this is a bug.

>>> b = b'abcde'
>>> m = memoryview(b)
>>> list(m)
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    list(m)
TypeError: 'memoryview' object is not iterable

The underlying problem is that the __getitem__ method is listed in the
as_mapping section instead of as_sequence.  This was necessary so that
the ellipsis could be supported (the mapping version accepts arbitrary
objects while the sequence version only accepts integer indices). 
Unfortunately, the logic for Objects/abstract.c::PySeq_Iter() expects to
find the getitem defined in s->ob_type->tp_as_sequence->sq_item slot.  

This patch attaches the appropriate code in that slot.  The code is a
simple cut and paste from the more general memory_subscript() function
listed just above.
msg89641 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2009-06-23 19:40
Given that the original code being copied is correct, it looks to me
like the revision is fine.  The memoryview tests pass for me with the
patch applied.
msg89644 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2009-06-23 21:00
Applied in r73531 and r73532.
History
Date User Action Args
2022-04-11 14:56:50adminsetgithub: 50578
2009-06-23 21:00:08rhettingersetstatus: open -> closed

messages: + msg89644
versions: + Python 2.7
2009-06-23 20:05:58rhettingersetfiles: + mview3.diff
2009-06-23 19:40:33r.david.murraysetassignee: r.david.murray -> rhettinger
resolution: accepted
messages: + msg89641
2009-06-23 19:34:40rhettingersetfiles: + mview2.diff
2009-06-23 18:40:00rhettingercreate