Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Changes from review
  • Loading branch information
colesbury committed Jan 30, 2024
commit b91363f838de276402eb5ea1d523a62a89cb4640
20 changes: 9 additions & 11 deletions Doc/c-api/list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,25 @@ List Objects
Similar to :c:func:`PyList_Size`, but without error checking.


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

Return 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)),
Comment thread
colesbury marked this conversation as resolved.
Outdated
return ``NULL`` and set an :exc:`IndexError` exception.

.. versionadded:: 3.13

.. 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.
.. c:function:: PyObject* PyList_GetItem(PyObject *list, Py_ssize_t index)

This behaves like :c:func:`PyList_GetItem`, but returns a
:term:`strong reference` instead of a :term:`borrowed reference`.
Return 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.

.. versionadded:: 3.13
This behaves like :c:func:`PyList_GetItemRef`, but returns a
:term:`borrowed reference` instead of a :term:`strong reference`.


.. c:function:: PyObject* PyList_GET_ITEM(PyObject *list, Py_ssize_t i)
Expand Down
31 changes: 13 additions & 18 deletions Lib/test/test_capi/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,28 @@ def test_list_get_size(self):
# CRASHES size(UserList())
# CRASHES size(NULL)


def test_list_getitem(self):
# Test PyList_GetItem()
getitem = _testcapi.list_getitem
def check_list_get_item(self, getitem, exctype):
# Common test cases for PyList_GetItem() and PyList_GetItemRef()
lst = [1, 2, 3]
self.assertEqual(getitem(lst, 0), 1)
self.assertEqual(getitem(lst, 2), 3)
self.assertRaises(IndexError, getitem, lst, 3)
self.assertRaises(IndexError, getitem, lst, -1)
self.assertRaises(IndexError, getitem, lst, PY_SSIZE_T_MIN)
self.assertRaises(IndexError, getitem, lst, PY_SSIZE_T_MAX)
self.assertRaises(SystemError, getitem, 42, 1)
self.assertRaises(SystemError, getitem, (1, 2, 3), 1)
self.assertRaises(SystemError, getitem, {1: 2}, 1)

self.assertRaises(exctype, getitem, 42, 1)
self.assertRaises(exctype, getitem, (1, 2, 3), 1)
self.assertRaises(exctype, getitem, {1: 2}, 1)
# CRASHES getitem(NULL, 1)

def test_list_getitem(self):
# Test PyList_GetItem()
self.check_list_get_item(_testcapi.list_getitem, SystemError)

def test_list_get_item_ref(self):
# Test PyList_GetItemRef()
self.check_list_get_item(_testcapi.list_get_item_ref, TypeError)

def test_list_get_item(self):
# Test PyList_GET_ITEM()
get_item = _testcapi.list_get_item
Expand All @@ -112,16 +117,6 @@ def test_list_get_item(self):
# CRASHES get_item(21, 2)
# CRASHES get_item(NULL, 1)

def test_list_get_item_ref(self):
# 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()
setitem = _testcapi.list_setitem
Expand Down
4 changes: 2 additions & 2 deletions Misc/stable_abi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -905,8 +905,6 @@
added = '3.2'
[function.PyList_GetItem]
added = '3.2'
[function.PyList_GetItemRef]
added = '3.13'
[function.PyList_GetSlice]
added = '3.2'
[function.PyList_Insert]
Expand Down Expand Up @@ -2483,3 +2481,5 @@
[function._Py_SetRefcnt]
added = '3.13'
abi_only = true
[function.PyList_GetItemRef]
added = '3.13'