From db088ecb22eb25b1e86f6edae2330e2e2d76d43a Mon Sep 17 00:00:00 2001 From: Brendan <2bndy5@gmail.com> Date: Tue, 14 Oct 2025 23:11:18 -0700 Subject: [PATCH] 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 --- src/repository.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/repository.c b/src/repository.c index 4be614bf..1e57d8f5 100644 --- a/src/repository.c +++ b/src/repository.c @@ -46,6 +46,17 @@ #include #include +// TODO: remove this function when Python 3.13 becomes the minimum supported version +#if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 13 +static inline PyObject * +PyList_GetItemRef(PyObject *op, Py_ssize_t index) +{ + PyObject *item = PyList_GetItem(op, index); + Py_XINCREF(item); + return item; +} +#endif + extern PyObject *GitError; extern PyTypeObject IndexType; @@ -599,8 +610,11 @@ merge_base_xxx(Repository *self, PyObject *args, git_merge_base_xxx_t git_merge_ } for (; i < commit_oid_count; i++) { - py_commit_oid = PyList_GET_ITEM(py_commit_oids, i); + py_commit_oid = PyList_GetItemRef(py_commit_oids, i); + if (py_commit_oid == NULL) + goto out; err = py_oid_to_git_oid_expand(self->repo, py_commit_oid, &commit_oids[i]); + Py_DECREF(py_commit_oid); if (err < 0) goto out; } @@ -1052,8 +1066,11 @@ Repository_create_commit(Repository *self, PyObject *args) goto out; } for (; i < parent_count; i++) { - py_parent = PyList_GET_ITEM(py_parents, i); + py_parent = PyList_GetItemRef(py_parents, i); + if (py_parent == NULL) + goto out; len = py_oid_to_git_oid(py_parent, &oid); + Py_DECREF(py_parent); if (len == 0) goto out; err = git_commit_lookup_prefix(&parents[i], self->repo, &oid, len); @@ -1135,8 +1152,11 @@ Repository_create_commit_string(Repository *self, PyObject *args) goto out; } for (; i < parent_count; i++) { - py_parent = PyList_GET_ITEM(py_parents, i); + py_parent = PyList_GetItemRef(py_parents, i); + if (py_parent == NULL) + goto out; len = py_oid_to_git_oid(py_parent, &oid); + Py_DECREF(py_parent); if (len == 0) goto out; err = git_commit_lookup_prefix(&parents[i], self->repo, &oid, len);