Skip to content

Commit 48c178f

Browse files
committed
fix: replace PyList_GET_ITEM()
resolves #1434 Instead use [`PyList_GetItemRef()`] for python >= 3.13. Internally, this function is conditionally defined for python < 3.13. [`PyList_GetItemRef()`] is part of the Stable ABI but also propagate errors. Whereas the previous [`PyList_GET_ITEM()`] did not do any error checking and was not part of the Stable ABI. [`PyList_GetItemRef()`]: https://docs.python.org/3/c-api/list.html#c.PyList_GetItemRef [`PyList_GET_ITEM()`]: https://docs.python.org/3/c-api/list.html#c.PyList_GET_ITEM
1 parent 2ea43ed commit 48c178f

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/repository.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@
4646
#include <git2/odb_backend.h>
4747
#include <git2/sys/repository.h>
4848

49+
// TODO: remove this function when Python 3.13 becomes the minimum supported version
50+
#if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 13
51+
static inline PyObject *
52+
PyList_GetItemRef(PyObject *op, Py_ssize_t index)
53+
{
54+
PyObject *item = PyList_GetItem(op, index);
55+
if (item != NULL)
56+
Py_IncRef(item);
57+
return item;
58+
}
59+
#endif
60+
4961
extern PyObject *GitError;
5062

5163
extern PyTypeObject IndexType;
@@ -599,8 +611,11 @@ merge_base_xxx(Repository *self, PyObject *args, git_merge_base_xxx_t git_merge_
599611
}
600612

601613
for (; i < commit_oid_count; i++) {
602-
py_commit_oid = PyList_GET_ITEM(py_commit_oids, i);
614+
py_commit_oid = PyList_GetItemRef(py_commit_oids, i);
615+
if (py_commit_oid == NULL)
616+
goto out;
603617
err = py_oid_to_git_oid_expand(self->repo, py_commit_oid, &commit_oids[i]);
618+
Py_DecRef(py_commit_oid);
604619
if (err < 0)
605620
goto out;
606621
}
@@ -1052,8 +1067,11 @@ Repository_create_commit(Repository *self, PyObject *args)
10521067
goto out;
10531068
}
10541069
for (; i < parent_count; i++) {
1055-
py_parent = PyList_GET_ITEM(py_parents, i);
1070+
py_parent = PyList_GetItemRef(py_parents, i);
1071+
if (py_parent == NULL)
1072+
goto out;
10561073
len = py_oid_to_git_oid(py_parent, &oid);
1074+
Py_DecRef(py_parent);
10571075
if (len == 0)
10581076
goto out;
10591077
err = git_commit_lookup_prefix(&parents[i], self->repo, &oid, len);
@@ -1135,8 +1153,11 @@ Repository_create_commit_string(Repository *self, PyObject *args)
11351153
goto out;
11361154
}
11371155
for (; i < parent_count; i++) {
1138-
py_parent = PyList_GET_ITEM(py_parents, i);
1156+
py_parent = PyList_GetItemRef(py_parents, i);
1157+
if (py_parent == NULL)
1158+
goto out;
11391159
len = py_oid_to_git_oid(py_parent, &oid);
1160+
Py_DecRef(py_parent);
11401161
if (len == 0)
11411162
goto out;
11421163
err = git_commit_lookup_prefix(&parents[i], self->repo, &oid, len);

0 commit comments

Comments
 (0)