Skip to content

Commit 760fcca

Browse files
committed
Support PyTuple_FromArray() in the limited C API
1 parent c6dc4b5 commit 760fcca

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

‎pythoncapi_compat.h‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2784,7 +2784,7 @@ PyBytesWriter_Format(PyBytesWriter *writer, const char *format, ...)
27842784
#endif // PY_VERSION_HEX < 0x030F00A1
27852785

27862786

2787-
#if PY_VERSION_HEX < 0x030F00A1 && !defined(Py_LIMITED_API)
2787+
#if PY_VERSION_HEX < 0x030F00A1
27882788
static inline PyObject*
27892789
PyTuple_FromArray(PyObject *const *array, Py_ssize_t size)
27902790
{
@@ -2794,7 +2794,14 @@ PyTuple_FromArray(PyObject *const *array, Py_ssize_t size)
27942794
}
27952795
for (Py_ssize_t i=0; i < size; i++) {
27962796
PyObject *item = array[i];
2797+
#ifndef Py_LIMITED_API
27972798
PyTuple_SET_ITEM(tuple, i, Py_NewRef(item));
2799+
#else
2800+
if (PyTuple_SetItem(tuple, i, Py_NewRef(item)) < 0) {
2801+
Py_DECREF(tuple);
2802+
return NULL;
2803+
}
2804+
#endif
27982805
}
27992806
return tuple;
28002807
}

‎tests/test_pythoncapi_compat_cext.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2415,6 +2415,8 @@ test_tuple_fromarray(void)
24152415
PyLong_FromLong(2),
24162416
PyLong_FromLong(3)
24172417
};
2418+
assert(array[0] != NULL && array[1] != NULL && array[2] != NULL);
2419+
24182420
PyObject *tuple = PyTuple_FromArray(array, 3);
24192421
if (tuple == NULL) {
24202422
goto error;

0 commit comments

Comments
 (0)