diff --git a/docs/api.rst b/docs/api.rst index d4846db..a0b7ecf 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -121,6 +121,14 @@ Python 3.13 See `PyUnicode_EqualToUTF8AndSize() documentation `__. +.. c:function:: int PyList_Extend(PyObject *list, PyObject *iterable) + + See `PyList_Extend() documentation `__. + +.. c:function:: int PyList_Clear(PyObject *list) + + See `PyList_Clear() documentation `__. + Python 3.12 ----------- diff --git a/docs/changelog.rst b/docs/changelog.rst index 4147a4a..25e14d2 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -1,6 +1,11 @@ Changelog ========= +* 2023-11-13: Add functions: + + * ``PyList_Extend()`` + * ``PyList_Clear()`` + * 2023-10-04: Add functions: * ``PyUnicode_EqualToUTF8()`` diff --git a/pythoncapi_compat.h b/pythoncapi_compat.h index b423fbd..6c67426 100644 --- a/pythoncapi_compat.h +++ b/pythoncapi_compat.h @@ -1013,6 +1013,21 @@ PyUnicode_EqualToUTF8(PyObject *unicode, const char *str) #endif +// gh-111138 added PyList_Extend() and PyList_Clear() to Python 3.13.0a2 +#if PY_VERSION_HEX < 0x030D00A2 +static inline int +PyList_Extend(PyObject *list, PyObject *iterable) +{ + return PyList_SetSlice(list, PY_SSIZE_T_MAX, PY_SSIZE_T_MAX, iterable); +} + +static inline int +PyList_Clear(PyObject *list) +{ + return PyList_SetSlice(list, 0, PY_SSIZE_T_MAX, NULL); +} +#endif + #ifdef __cplusplus } #endif diff --git a/tests/test_pythoncapi_compat_cext.c b/tests/test_pythoncapi_compat_cext.c index 8d8ef38..81cfe6b 100644 --- a/tests/test_pythoncapi_compat_cext.c +++ b/tests/test_pythoncapi_compat_cext.c @@ -1395,6 +1395,33 @@ test_unicode(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) } +static PyObject * +test_list(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) +{ + PyObject *list = PyList_New(0); + if (list == NULL) { + return NULL; + } + + PyObject *abc = PyUnicode_FromString("abc"); + if (abc == NULL) { + return NULL; + } + + // test PyList_Extend() + assert(PyList_Extend(list, abc) == 0); + Py_DECREF(abc); + assert(PyList_GET_SIZE(list) == 3); + + // test PyList_Clear() + assert(PyList_Clear(list) == 0); + assert(PyList_GET_SIZE(list) == 0); + + Py_DECREF(list); + Py_RETURN_NONE; +} + + static struct PyMethodDef methods[] = { {"test_object", test_object, METH_NOARGS, _Py_NULL}, {"test_py_is", test_py_is, METH_NOARGS, _Py_NULL}, @@ -1425,6 +1452,7 @@ static struct PyMethodDef methods[] = { {"test_managed_dict", test_managed_dict, METH_NOARGS, _Py_NULL}, #endif {"test_unicode", test_unicode, METH_NOARGS, _Py_NULL}, + {"test_list", test_list, METH_NOARGS, _Py_NULL}, {_Py_NULL, _Py_NULL, 0, _Py_NULL} };