gh-83791: Raise TypeError for len(memoryview_0d) (#18463)

Changes the behaviour of `len` on a zero-dimensional `memoryview` to raise `TypeError`. Previously, `len` would return `1`.
This commit is contained in:
Eric Wieser 2023-04-22 17:32:47 +01:00 committed by GitHub
parent caed49448d
commit 3d2a46845b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 22 deletions

View file

@ -2642,7 +2642,11 @@ static Py_ssize_t
memory_length(PyMemoryViewObject *self)
{
CHECK_RELEASED_INT(self);
return self->view.ndim == 0 ? 1 : self->view.shape[0];
if (self->view.ndim == 0) {
PyErr_SetString(PyExc_TypeError, "0-dim memory has no length");
return -1;
}
return self->view.shape[0];
}
/* As mapping */