summaryrefslogtreecommitdiffstats
path: root/Objects
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 /Objects
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 'Objects')
-rw-r--r--Objects/listobject.c15
1 files changed, 15 insertions, 0 deletions
diff --git a/Objects/listobject.c b/Objects/listobject.c
index da2b9cc..82a4ba9 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -257,6 +257,21 @@ PyList_GetItem(PyObject *op, Py_ssize_t i)
return ((PyListObject *)op) -> ob_item[i];
}
+PyObject *
+PyList_GetItemRef(PyObject *op, Py_ssize_t i)
+{
+ if (!PyList_Check(op)) {
+ PyErr_SetString(PyExc_TypeError, "expected a list");
+ return NULL;
+ }
+ if (!valid_index(i, Py_SIZE(op))) {
+ _Py_DECLARE_STR(list_err, "list index out of range");
+ PyErr_SetObject(PyExc_IndexError, &_Py_STR(list_err));
+ return NULL;
+ }
+ return Py_NewRef(PyList_GET_ITEM(op, i));
+}
+
int
PyList_SetItem(PyObject *op, Py_ssize_t i,
PyObject *newitem)