[New-bugs-announce] [issue39610] memoryview.__len__ should raise an exception for 0d buffers

Eric Wieser report at bugs.python.org
Tue Feb 11 10:42:52 EST 2020

New submission from Eric Wieser <wieser.eric at gmail.com>:

Right now, the behavior is:

>>> import numpy as np
>>> arr_0d = np.array(42)
>>> mem_0d = memoryview(arr_0d)
>>> len(mem_0d)
>>> mem_0d[0]
TypeError: invalid indexing of 0-dim memory

It seems bizarre to have this object pretend to be a sequence when you ask for its length, yet not behave like one when you actually try to use this length. I'd suggest cpython should behave like numpy here, and fail:

>>> len(arr_0d)
TypeError: len() of unsized object

Perhaps `TypeError: cannot get length of 0-dim memory` would be more appropriate as a message.


Wasn't sure how to classify this, feel free to reclassify

components: Interpreter Core
messages: 361821
nosy: Eric Wieser, skrah
priority: normal
severity: normal
status: open
title: memoryview.__len__ should raise an exception for 0d buffers
type: behavior
versions: Python 3.5, Python 3.6, Python 3.7, Python 3.8, Python 3.9

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list