mirror of
https://github.com/python/cpython.git
synced 2025-12-04 00:30:19 +00:00
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:
parent
caed49448d
commit
3d2a46845b
5 changed files with 31 additions and 22 deletions
|
|
@ -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 */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue