Skip to content

Commit f667991

Browse files
committed
gh-88: Add PyList_GetItemRef()
1 parent d16872a commit f667991

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

docs/api.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ Python 3.13
170170
171171
See `PyTime_PerfCounter() documentation <https://docs.python.org/dev/c-api/time.html#c.PyTime_PerfCounter>`__.
172172
173+
.. c:function:: PyObject* PyList_GetItemRef(PyObject *op, Py_ssize_t index)
174+
175+
See `PyList_GetItemRef() documentation <https://docs.python.org/dev/c-api/list.html#c.PyList_GetItemRef>`__.
176+
173177
174178
Not supported:
175179

pythoncapi_compat.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,18 @@ static inline PyObject* Py_GetConstantBorrowed(unsigned int constant_id)
12861286
#endif
12871287

12881288

1289+
// gh-114329 added PyList_GetItemRef() to Python 3.13.0a4
1290+
#if PY_VERSION_HEX < 0x030D00A4
1291+
static inline PyObject *
1292+
PyList_GetItemRef(PyObject *op, Py_ssize_t index)
1293+
{
1294+
PyObject *item = PyList_GetItem(op, index);
1295+
Py_XINCREF(item);
1296+
return item;
1297+
}
1298+
#endif
1299+
1300+
12891301
#ifdef __cplusplus
12901302
}
12911303
#endif

tests/test_pythoncapi_compat_cext.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,15 +1480,24 @@ test_list(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
14801480
return NULL;
14811481
}
14821482

1483-
PyObject *abc = PyUnicode_FromString("abc");
1484-
if (abc == NULL) {
1485-
return NULL;
1483+
// test PyList_Extend()
1484+
{
1485+
PyObject *abc = PyUnicode_FromString("abc");
1486+
if (abc == NULL) {
1487+
Py_DECREF(list);
1488+
return NULL;
1489+
}
1490+
1491+
assert(PyList_Extend(list, abc) == 0);
1492+
Py_DECREF(abc);
1493+
assert(PyList_GET_SIZE(list) == 3);
14861494
}
14871495

1488-
// test PyList_Extend()
1489-
assert(PyList_Extend(list, abc) == 0);
1490-
Py_DECREF(abc);
1491-
assert(PyList_GET_SIZE(list) == 3);
1496+
// test PyList_GetItemRef()
1497+
PyObject *item = PyList_GetItemRef(list, 1);
1498+
assert(item != NULL);
1499+
assert(item == PyList_GetItem(list, 1));
1500+
Py_DECREF(item);
14921501

14931502
// test PyList_Clear()
14941503
assert(PyList_Clear(list) == 0);

0 commit comments

Comments
 (0)