[New-bugs-announce] [issue6329] Fix iteration for memoryviews

Raymond Hettinger report at bugs.python.org
Tue Jun 23 20:40:02 CEST 2009

New submission from Raymond Hettinger <rhettinger at users.sourceforge.net>:

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>
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.

assignee: r.david.murray
components: Interpreter Core
files: mview.diff
keywords: patch
messages: 89637
nosy: r.david.murray, rhettinger
priority: high
severity: normal
stage: patch review
status: open
title: Fix iteration for memoryviews
type: behavior
versions: Python 3.1
Added file: http://bugs.python.org/file14344/mview.diff

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list