summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorBénédikt Tran <10796600+picnixz@users.noreply.github.com>2024-12-10 02:48:38 (GMT)
committerGitHub <noreply@github.com>2024-12-10 02:48:38 (GMT)
commit58c753827ac7aa3d7f1495ac206c28bf2f6c67e8 (patch)
treee8aeddd9663ea5a9ef0bd6af10d1e19d9a73db30 /Objects
parent3b18af964da9814474a5db9e502962c7e0593e8d (diff)
downloadcpython-58c753827ac7aa3d7f1495ac206c28bf2f6c67e8.zip
cpython-58c753827ac7aa3d7f1495ac206c28bf2f6c67e8.tar.gz
cpython-58c753827ac7aa3d7f1495ac206c28bf2f6c67e8.tar.bz2
gh-125420: implement `Sequence.index` API on `memoryview` objects (#125446)
Diffstat (limited to 'Objects')
-rw-r--r--Objects/clinic/memoryobject.c.h48
-rw-r--r--Objects/memoryobject.c87
2 files changed, 134 insertions, 1 deletions
diff --git a/Objects/clinic/memoryobject.c.h b/Objects/clinic/memoryobject.c.h
index 185d781..9b8a233 100644
--- a/Objects/clinic/memoryobject.c.h
+++ b/Objects/clinic/memoryobject.c.h
@@ -418,4 +418,50 @@ skip_optional_pos:
exit:
return return_value;
}
-/*[clinic end generated code: output=0a93f08110630633 input=a9049054013a1b77]*/
+
+PyDoc_STRVAR(memoryview_index__doc__,
+"index($self, value, start=0, stop=sys.maxsize, /)\n"
+"--\n"
+"\n"
+"Return the index of the first occurrence of a value.\n"
+"\n"
+"Raises ValueError if the value is not present.");
+
+#define MEMORYVIEW_INDEX_METHODDEF \
+ {"index", _PyCFunction_CAST(memoryview_index), METH_FASTCALL, memoryview_index__doc__},
+
+static PyObject *
+memoryview_index_impl(PyMemoryViewObject *self, PyObject *value,
+ Py_ssize_t start, Py_ssize_t stop);
+
+static PyObject *
+memoryview_index(PyMemoryViewObject *self, PyObject *const *args, Py_ssize_t nargs)
+{
+ PyObject *return_value = NULL;
+ PyObject *value;
+ Py_ssize_t start = 0;
+ Py_ssize_t stop = PY_SSIZE_T_MAX;
+
+ if (!_PyArg_CheckPositional("index", nargs, 1, 3)) {
+ goto exit;
+ }
+ value = args[0];
+ if (nargs < 2) {
+ goto skip_optional;
+ }
+ if (!_PyEval_SliceIndexNotNone(args[1], &start)) {
+ goto exit;
+ }
+ if (nargs < 3) {
+ goto skip_optional;
+ }
+ if (!_PyEval_SliceIndexNotNone(args[2], &stop)) {
+ goto exit;
+ }
+skip_optional:
+ return_value = memoryview_index_impl(self, value, start, stop);
+
+exit:
+ return return_value;
+}
+/*[clinic end generated code: output=2742d371dba7314f input=a9049054013a1b77]*/
diff --git a/Objects/memoryobject.c b/Objects/memoryobject.c
index 25634f9..345aa79 100644
--- a/Objects/memoryobject.c
+++ b/Objects/memoryobject.c
@@ -2749,6 +2749,92 @@ static PySequenceMethods memory_as_sequence = {
/**************************************************************************/
+/* Lookup */
+/**************************************************************************/
+
+/*[clinic input]
+memoryview.index
+
+ value: object
+ start: slice_index(accept={int}) = 0
+ stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
+ /
+
+Return the index of the first occurrence of a value.
+
+Raises ValueError if the value is not present.
+[clinic start generated code]*/
+
+static PyObject *
+memoryview_index_impl(PyMemoryViewObject *self, PyObject *value,
+ Py_ssize_t start, Py_ssize_t stop)
+/*[clinic end generated code: output=e0185e3819e549df input=0697a0165bf90b5a]*/
+{
+ const Py_buffer *view = &self->view;
+ CHECK_RELEASED(self);
+
+ if (view->ndim == 0) {
+ PyErr_SetString(PyExc_TypeError, "invalid lookup on 0-dim memory");
+ return NULL;
+ }
+
+ if (view->ndim == 1) {
+ Py_ssize_t n = view->shape[0];
+
+ if (start < 0) {
+ start = Py_MAX(start + n, 0);
+ }
+
+ if (stop < 0) {
+ stop = Py_MAX(stop + n, 0);
+ }
+
+ stop = Py_MIN(stop, n);
+ assert(stop >= 0);
+ assert(stop <= n);
+
+ start = Py_MIN(start, stop);
+ assert(0 <= start);
+ assert(start <= stop);
+
+ PyObject *obj = _PyObject_CAST(self);
+ for (Py_ssize_t index = start; index < stop; index++) {
+ // Note: while memoryviews can be mutated during iterations
+ // when calling the == operator, their shape cannot. As such,
+ // it is safe to assume that the index remains valid for the
+ // entire loop.
+ assert(index < n);
+
+ PyObject *item = memory_item(obj, index);
+ if (item == NULL) {
+ return NULL;
+ }
+ if (item == value) {
+ Py_DECREF(item);
+ return PyLong_FromSsize_t(index);
+ }
+ int contained = PyObject_RichCompareBool(item, value, Py_EQ);
+ Py_DECREF(item);
+ if (contained > 0) { // more likely than 'contained < 0'
+ return PyLong_FromSsize_t(index);
+ }
+ else if (contained < 0) {
+ return NULL;
+ }
+ }
+
+ PyErr_SetString(PyExc_ValueError, "memoryview.index(x): x not found");
+ return NULL;
+ }
+
+ PyErr_SetString(PyExc_NotImplementedError,
+ "multi-dimensional lookup is not implemented");
+ return NULL;
+
+}
+
+
+/**************************************************************************/
/* Comparisons */
/**************************************************************************/
@@ -3284,6 +3370,7 @@ static PyMethodDef memory_methods[] = {
MEMORYVIEW_CAST_METHODDEF
MEMORYVIEW_TOREADONLY_METHODDEF
MEMORYVIEW__FROM_FLAGS_METHODDEF
+ MEMORYVIEW_INDEX_METHODDEF
{"__enter__", memory_enter, METH_NOARGS, NULL},
{"__exit__", memory_exit, METH_VARARGS, memory_exit_doc},
{"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},