Skip to content

Commit 6e3459d

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 6e3459d

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/repository.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@
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+
Py_XINCREF(item);
56+
return item;
57+
}
58+
#endif
59+
4960
extern PyObject *GitError;
5061

5162
extern PyTypeObject IndexType;
@@ -599,8 +610,11 @@ merge_base_xxx(Repository *self, PyObject *args, git_merge_base_xxx_t git_merge_
599610
}
600611

601612
for (; i < commit_oid_count; i++) {
602-
py_commit_oid = PyList_GET_ITEM(py_commit_oids, i);
613+
py_commit_oid = PyList_GetItemRef(py_commit_oids, i);
614+
if (py_commit_oid == NULL)
615+
goto out;
603616
err = py_oid_to_git_oid_expand(self->repo, py_commit_oid, &commit_oids[i]);
617+
Py_DECREF(py_commit_oid);
604618
if (err < 0)
605619
goto out;
606620
}
@@ -1052,8 +1066,11 @@ Repository_create_commit(Repository *self, PyObject *args)
10521066
goto out;
10531067
}
10541068
for (; i < parent_count; i++) {
1055-
py_parent = PyList_GET_ITEM(py_parents, i);
1069+
py_parent = PyList_GetItemRef(py_parents, i);
1070+
if (py_parent == NULL)
1071+
goto out;
10561072
len = py_oid_to_git_oid(py_parent, &oid);
1073+
Py_DECREF(py_parent);
10571074
if (len == 0)
10581075
goto out;
10591076
err = git_commit_lookup_prefix(&parents[i], self->repo, &oid, len);
@@ -1135,8 +1152,11 @@ Repository_create_commit_string(Repository *self, PyObject *args)
11351152
goto out;
11361153
}
11371154
for (; i < parent_count; i++) {
1138-
py_parent = PyList_GET_ITEM(py_parents, i);
1155+
py_parent = PyList_GetItemRef(py_parents, i);
1156+
if (py_parent == NULL)
1157+
goto out;
11391158
len = py_oid_to_git_oid(py_parent, &oid);
1159+
Py_DECREF(py_parent);
11401160
if (len == 0)
11411161
goto out;
11421162
err = git_commit_lookup_prefix(&parents[i], self->repo, &oid, len);

0 commit comments

Comments
 (0)