summaryrefslogtreecommitdiffstats
path: root/Modules/_testcapi
diff options
context:
space:
mode:
authorSam Gross <colesbury@gmail.com>2024-02-02 13:03:15 (GMT)
committerGitHub <noreply@github.com>2024-02-02 13:03:15 (GMT)
commitd0f1307580a69372611d27b04bbf2551dc85a1ef (patch)
treec690e6915eb44d57fc8af1623dc57babe3eca3ab /Modules/_testcapi
parent0e71a295e9530c939a5efcb45db23cf31e0303b4 (diff)
downloadcpython-d0f1307580a69372611d27b04bbf2551dc85a1ef.zip
cpython-d0f1307580a69372611d27b04bbf2551dc85a1ef.tar.gz
cpython-d0f1307580a69372611d27b04bbf2551dc85a1ef.tar.bz2
gh-114329: Add `PyList_GetItemRef` function (GH-114504)
The new `PyList_GetItemRef` is similar to `PyList_GetItem`, but returns a strong reference instead of a borrowed reference. Additionally, if the passed "list" object is not a list, the function sets a `TypeError` instead of calling `PyErr_BadInternalCall()`.
Diffstat (limited to 'Modules/_testcapi')
-rw-r--r--Modules/_testcapi/list.c13
1 files changed, 13 insertions, 0 deletions
diff --git a/Modules/_testcapi/list.c b/Modules/_testcapi/list.c
index 10e1869..2cb6499 100644
--- a/Modules/_testcapi/list.c
+++ b/Modules/_testcapi/list.c
@@ -60,6 +60,18 @@ list_get_item(PyObject *Py_UNUSED(module), PyObject *args)
}
static PyObject *
+list_get_item_ref(PyObject *Py_UNUSED(module), PyObject *args)
+{
+ PyObject *obj;
+ Py_ssize_t i;
+ if (!PyArg_ParseTuple(args, "On", &obj, &i)) {
+ return NULL;
+ }
+ NULLABLE(obj);
+ return PyList_GetItemRef(obj, i);
+}
+
+static PyObject *
list_setitem(PyObject *Py_UNUSED(module), PyObject *args)
{
PyObject *obj, *value;
@@ -191,6 +203,7 @@ static PyMethodDef test_methods[] = {
{"list_get_size", list_get_size, METH_O},
{"list_getitem", list_getitem, METH_VARARGS},
{"list_get_item", list_get_item, METH_VARARGS},
+ {"list_get_item_ref", list_get_item_ref, METH_VARARGS},
{"list_setitem", list_setitem, METH_VARARGS},
{"list_set_item", list_set_item, METH_VARARGS},
{"list_insert", list_insert, METH_VARARGS},