summaryrefslogtreecommitdiffstats
path: root/Objects/memoryobject.c
diff options
context:
space:
mode:
authorEric Wieser <wieser.eric@gmail.com>2023-04-22 16:32:47 (GMT)
committerGitHub <noreply@github.com>2023-04-22 16:32:47 (GMT)
commit3d2a46845b67407e2436d910b2e1740c34f4f10d (patch)
tree607d43aa0a30a293fcffc2623453cc7787687edd /Objects/memoryobject.c
parentcaed49448d195565940caf198cf0edda65ee5679 (diff)
downloadcpython-3d2a46845b67407e2436d910b2e1740c34f4f10d.zip
cpython-3d2a46845b67407e2436d910b2e1740c34f4f10d.tar.gz
cpython-3d2a46845b67407e2436d910b2e1740c34f4f10d.tar.bz2
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`.
Diffstat (limited to 'Objects/memoryobject.c')
-rw-r--r--Objects/memoryobject.c6
1 files changed, 5 insertions, 1 deletions
diff --git a/Objects/memoryobject.c b/Objects/memoryobject.c
index 1d6cc3b..34cc797 100644
--- a/Objects/memoryobject.c
+++ b/Objects/memoryobject.c
@@ -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 */