Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-114329: Add PyList_GetItemRef function #114504

Merged
merged 6 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Doc/c-api/list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ List Objects
return ``NULL`` and set an :exc:`IndexError` exception.


.. c:function:: PyObject* PyList_GetItemRef(PyObject *list, Py_ssize_t index)

Return a :term:`strong reference` to the object at position *index* in the
list pointed to by *list*. The position must be non-negative; indexing from
the end of the list is not supported. If *index* is out of bounds
(<0 or >=len(list)), return ``NULL`` and set an :exc:`IndexError` exception.
If *list* is not a :class:`list` object, return ``NULL`` and set a
:exc:`TypeError` exception.

This behaves like :c:func:`PyList_GetItem`, but returns a
:term:`strong reference` instead of a :term:`borrowed reference`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to make this function the new reference, and document PyList_GetItem() as: "Similar to PyList_GetItemRef(), but return a borrowed reference".


.. versionadded:: 3.13


.. c:function:: PyObject* PyList_GET_ITEM(PyObject *list, Py_ssize_t i)

Similar to :c:func:`PyList_GetItem`, but without error checking.
Expand Down
4 changes: 4 additions & 0 deletions Doc/data/refcounts.dat
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,10 @@ PyList_GetItem:PyObject*::0:
PyList_GetItem:PyObject*:list:0:
PyList_GetItem:Py_ssize_t:index::

PyList_GetItemRef:PyObject*::+1:
PyList_GetItemRef:PyObject*:list:0:
PyList_GetItemRef:Py_ssize_t:index::

PyList_GetSlice:PyObject*::+1:
PyList_GetSlice:PyObject*:list:0:
PyList_GetSlice:Py_ssize_t:low::
Expand Down
1 change: 1 addition & 0 deletions Doc/data/stable_abi.dat

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1305,6 +1305,10 @@ New Features
UTF-8 encoded bytes string, rather than a :c:expr:`PyObject*`.
(Contributed by Victor Stinner in :gh:`108314`.)

* Added :c:func:`PyList_GetItemRef` function: similar to
:c:func:`PyList_GetItem` but returns a :term:`strong reference` instead of
a :term:`borrowed reference`.

* Add :c:func:`Py_IsFinalizing` function: check if the main Python interpreter is
:term:`shutting down <interpreter shutdown>`.
(Contributed by Victor Stinner in :gh:`108014`.)
Expand Down
1 change: 1 addition & 0 deletions Include/listobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ PyAPI_FUNC(PyObject *) PyList_New(Py_ssize_t size);
PyAPI_FUNC(Py_ssize_t) PyList_Size(PyObject *);

PyAPI_FUNC(PyObject *) PyList_GetItem(PyObject *, Py_ssize_t);
PyAPI_FUNC(PyObject *) PyList_GetItemRef(PyObject *, Py_ssize_t);
PyAPI_FUNC(int) PyList_SetItem(PyObject *, Py_ssize_t, PyObject *);
PyAPI_FUNC(int) PyList_Insert(PyObject *, Py_ssize_t, PyObject *);
PyAPI_FUNC(int) PyList_Append(PyObject *, PyObject *);
Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_capi/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ def test_list_get_item(self):
# CRASHES get_item(21, 2)
# CRASHES get_item(NULL, 1)

def test_list_get_item_ref(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you share most code between test_list_get_item() and test_list_get_item_ref()?

# Test PyList_GetItemRef()
get_item_ref = _testcapi.list_get_item_ref
lst = [1, 2, [1, 2, 3]]
self.assertEqual(get_item_ref(lst, 0), 1)
self.assertEqual(get_item_ref(lst, 2), [1, 2, 3])
self.assertRaises(IndexError, get_item_ref, lst, 3)
self.assertRaises(IndexError, get_item_ref, lst, -1)
self.assertRaises(TypeError, get_item_ref, "not a list", 0)

def test_list_setitem(self):
# Test PyList_SetItem()
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_stable_abi_ctypes.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Add :c:func:`PyList_GetItemRef`, which is similar to
:c:func:`PyList_GetItem` but returns a :term:`strong reference` instead of a
:term:`borrowed reference`.
2 changes: 2 additions & 0 deletions Misc/stable_abi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,8 @@
added = '3.2'
[function.PyList_GetItem]
added = '3.2'
[function.PyList_GetItemRef]
added = '3.13'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move it to the end of the file.

[function.PyList_GetSlice]
added = '3.2'
[function.PyList_Insert]
Expand Down
13 changes: 13 additions & 0 deletions Modules/_testcapi/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ list_get_item(PyObject *Py_UNUSED(module), PyObject *args)
return Py_XNewRef(PyList_GET_ITEM(obj, i));
}

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)
{
Expand Down Expand Up @@ -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},
Expand Down
15 changes: 15 additions & 0 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,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");
encukou marked this conversation as resolved.
Show resolved Hide resolved
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)
Expand Down
1 change: 1 addition & 0 deletions PC/python3dll.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading