| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 49b740f commit 445e147
12 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -56,13 +56,21 @@ List Objects | |||
| 56 | 56 | Similar to :c:func:`PyList_Size`, but without error checking. | |
| 57 | 57 | ||
| 58 | 58 | ||
| 59 | - .. c:function:: PyObject* PyList_GetItem(PyObject *list, Py_ssize_t index) | ||
| 59 | + .. c:function:: PyObject* PyList_GetItemRef(PyObject *list, Py_ssize_t index) | ||
| 60 | 60 | ||
| 61 | 61 | Return the object at position *index* in the list pointed to by *list*. The | |
| 62 | 62 | position must be non-negative; indexing from the end of the list is not | |
| 63 | - supported. If *index* is out of bounds (<0 or >=len(list)), | ||
| 63 | + supported. If *index* is out of bounds (:code:`<0 or >=len(list)`), | ||
| 64 | 64 | return ``NULL`` and set an :exc:`IndexError` exception. | |
| 65 | 65 | ||
| 66 | + .. versionadded:: 3.13 | ||
| 67 | + | ||
| 68 | + | ||
| 69 | + .. c:function:: PyObject* PyList_GetItem(PyObject *list, Py_ssize_t index) | ||
| 70 | + | ||
| 71 | + Like :c:func:`PyList_GetItemRef`, but returns a | ||
| 72 | + :term:`borrowed reference` instead of a :term:`strong reference`. | ||
| 73 | + | ||
| 66 | 74 | ||
| 67 | 75 | .. c:function:: PyObject* PyList_GET_ITEM(PyObject *list, Py_ssize_t i) | |
| 68 | 76 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1133,6 +1133,10 @@ PyList_GetItem:PyObject*::0: | |||
| 1133 | 1133 | PyList_GetItem:PyObject*:list:0: | |
| 1134 | 1134 | PyList_GetItem:Py_ssize_t:index:: | |
| 1135 | 1135 | ||
| 1136 | + PyList_GetItemRef:PyObject*::+1: | ||
| 1137 | + PyList_GetItemRef:PyObject*:list:0: | ||
| 1138 | + PyList_GetItemRef:Py_ssize_t:index:: | ||
| 1139 | + | ||
| 1136 | 1140 | PyList_GetSlice:PyObject*::+1: | |
| 1137 | 1141 | PyList_GetSlice:PyObject*:list:0: | |
| 1138 | 1142 | PyList_GetSlice:Py_ssize_t:low:: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1376,6 +1376,10 @@ New Features | |||
| 1376 | 1376 | UTF-8 encoded bytes string, rather than a :c:expr:`PyObject*`. | |
| 1377 | 1377 | (Contributed by Victor Stinner in :gh:`108314`.) | |
| 1378 | 1378 | ||
| 1379 | + * Added :c:func:`PyList_GetItemRef` function: similar to | ||
| 1380 | + :c:func:`PyList_GetItem` but returns a :term:`strong reference` instead of | ||
| 1381 | + a :term:`borrowed reference`. | ||
| 1382 | + | ||
| 1379 | 1383 | * Add :c:func:`Py_IsFinalizing` function: check if the main Python interpreter is | |
| 1380 | 1384 | :term:`shutting down <interpreter shutdown>`. | |
| 1381 | 1385 | (Contributed by Victor Stinner in :gh:`108014`.) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -29,6 +29,7 @@ PyAPI_FUNC(PyObject *) PyList_New(Py_ssize_t size); | |||
| 29 | 29 | PyAPI_FUNC(Py_ssize_t) PyList_Size(PyObject *); | |
| 30 | 30 | ||
| 31 | 31 | PyAPI_FUNC(PyObject *) PyList_GetItem(PyObject *, Py_ssize_t); | |
| 32 | + PyAPI_FUNC(PyObject *) PyList_GetItemRef(PyObject *, Py_ssize_t); | ||
| 32 | 33 | PyAPI_FUNC(int) PyList_SetItem(PyObject *, Py_ssize_t, PyObject *); | |
| 33 | 34 | PyAPI_FUNC(int) PyList_Insert(PyObject *, Py_ssize_t, PyObject *); | |
| 34 | 35 | PyAPI_FUNC(int) PyList_Append(PyObject *, PyObject *); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -82,23 +82,28 @@ def test_list_get_size(self): | |||
| 82 | 82 | # CRASHES size(UserList()) | |
| 83 | 83 | # CRASHES size(NULL) | |
| 84 | 84 | ||
| 85 | - | ||
| 86 | - def test_list_getitem(self): | ||
| 87 | - # Test PyList_GetItem() | ||
| 88 | - getitem = _testcapi.list_getitem | ||
| 85 | + def check_list_get_item(self, getitem, exctype): | ||
| 86 | + # Common test cases for PyList_GetItem() and PyList_GetItemRef() | ||
| 89 | 87 | lst = [1, 2, 3] | |
| 90 | 88 | self.assertEqual(getitem(lst, 0), 1) | |
| 91 | 89 | self.assertEqual(getitem(lst, 2), 3) | |
| 92 | 90 | self.assertRaises(IndexError, getitem, lst, 3) | |
| 93 | 91 | self.assertRaises(IndexError, getitem, lst, -1) | |
| 94 | 92 | self.assertRaises(IndexError, getitem, lst, PY_SSIZE_T_MIN) | |
| 95 | 93 | self.assertRaises(IndexError, getitem, lst, PY_SSIZE_T_MAX) | |
| 96 | - self.assertRaises(SystemError, getitem, 42, 1) | ||
| 97 | - self.assertRaises(SystemError, getitem, (1, 2, 3), 1) | ||
| 98 | - self.assertRaises(SystemError, getitem, {1: 2}, 1) | ||
| 99 | - | ||
| 94 | + self.assertRaises(exctype, getitem, 42, 1) | ||
| 95 | + self.assertRaises(exctype, getitem, (1, 2, 3), 1) | ||
| 96 | + self.assertRaises(exctype, getitem, {1: 2}, 1) | ||
| 100 | 97 | # CRASHES getitem(NULL, 1) | |
| 101 | 98 | ||
| 99 | + def test_list_getitem(self): | ||
| 100 | + # Test PyList_GetItem() | ||
| 101 | + self.check_list_get_item(_testcapi.list_getitem, SystemError) | ||
| 102 | + | ||
| 103 | + def test_list_get_item_ref(self): | ||
| 104 | + # Test PyList_GetItemRef() | ||
| 105 | + self.check_list_get_item(_testcapi.list_get_item_ref, TypeError) | ||
| 106 | + | ||
| 102 | 107 | def test_list_get_item(self): | |
| 103 | 108 | # Test PyList_GET_ITEM() | |
| 104 | 109 | get_item = _testcapi.list_get_item | |
@@ -112,7 +117,6 @@ def test_list_get_item(self): | |||
| 112 | 117 | # CRASHES get_item(21, 2) | |
| 113 | 118 | # CRASHES get_item(NULL, 1) | |
| 114 | 119 | ||
| 115 | - | ||
| 116 | 120 | def test_list_setitem(self): | |
| 117 | 121 | # Test PyList_SetItem() | |
| 118 | 122 | setitem = _testcapi.list_setitem | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,3 @@ | |||
| 1 | + Add :c:func:`PyList_GetItemRef`, which is similar to | ||
| 2 | + :c:func:`PyList_GetItem` but returns a :term:`strong reference` instead of a | ||
| 3 | + :term:`borrowed reference`. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2487,3 +2487,5 @@ | |||
| 2487 | 2487 | abi_only = true | |
| 2488 | 2488 | [data.PyExc_IncompleteInputError] | |
| 2489 | 2489 | added = '3.13' | |
| 2490 | + [function.PyList_GetItemRef] | ||
| 2491 | + added = '3.13' | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -59,6 +59,18 @@ list_get_item(PyObject *Py_UNUSED(module), PyObject *args) | |||
| 59 | 59 | return Py_XNewRef(PyList_GET_ITEM(obj, i)); | |
| 60 | 60 | } | |
| 61 | 61 | ||
| 62 | + static PyObject * | ||
| 63 | + list_get_item_ref(PyObject *Py_UNUSED(module), PyObject *args) | ||
| 64 | + { | ||
| 65 | + PyObject *obj; | ||
| 66 | + Py_ssize_t i; | ||
| 67 | + if (!PyArg_ParseTuple(args, "On", &obj, &i)) { | ||
| 68 | + return NULL; | ||
| 69 | + } | ||
| 70 | + NULLABLE(obj); | ||
| 71 | + return PyList_GetItemRef(obj, i); | ||
| 72 | + } | ||
| 73 | + | ||
| 62 | 74 | static PyObject * | |
| 63 | 75 | list_setitem(PyObject *Py_UNUSED(module), PyObject *args) | |
| 64 | 76 | { | |
@@ -191,6 +203,7 @@ static PyMethodDef test_methods[] = { | |||
| 191 | 203 | {"list_get_size", list_get_size, METH_O}, | |
| 192 | 204 | {"list_getitem", list_getitem, METH_VARARGS}, | |
| 193 | 205 | {"list_get_item", list_get_item, METH_VARARGS}, | |
| 206 | + {"list_get_item_ref", list_get_item_ref, METH_VARARGS}, | ||
| 194 | 207 | {"list_setitem", list_setitem, METH_VARARGS}, | |
| 195 | 208 | {"list_set_item", list_set_item, METH_VARARGS}, | |
| 196 | 209 | {"list_insert", list_insert, METH_VARARGS}, | |
| Back | FazBrowse Home | New Git URL |
0 commit comments