classification
Title: memoryview.__len__ should raise an exception for 0d buffers
Type: behavior Stage: patch review
Components: Interpreter Core Versions: Python 3.9, Python 3.8, Python 3.7, Python 3.6, Python 3.5
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Eric Wieser, skrah
Priority: normal Keywords: patch

Created on 2020-02-11 15:42 by Eric Wieser, last changed 2020-02-12 00:00 by skrah.

Pull Requests
URL Status Linked Edit
PR 18463 open Eric.Wieser, 2020-02-11 15:51
Messages (2)
msg361821 - (view) Author: Eric Wieser (Eric Wieser) Date: 2020-02-11 15:42
Right now, the behavior is:

>>> import numpy as np
>>> arr_0d = np.array(42)
>>> mem_0d = memoryview(arr_0d)
>>> len(mem_0d)
1
>>> 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
msg361840 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2020-02-12 00:00
The change looks reasonable, but unfortunately this is a long-standing
behavior that originates from before the Python-3.3 memoryview rewrite.

It is also present in 2.7 (the previous implementation) and documented
in 3.3:

https://docs.python.org/3/library/stdtypes.html#memoryview


I think I documented it because it looked strange back then, too.
So I've to think a bit what will be affected and how this behavior
came into place.
History
Date User Action Args
2020-02-12 00:00:50skrahsetmessages: + msg361840
2020-02-11 15:51:35Eric.Wiesersetkeywords: + patch
stage: patch review
pull_requests: + pull_request17836
2020-02-11 15:42:52Eric Wiesercreate