From 6bfb71b0d2d1bcc792dd4e2257578f17cb88dc4a Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 13 Nov 2023 17:29:55 +0100 Subject: [PATCH] Add PyList_Extend() --- docs/api.rst | 8 ++++++++ docs/changelog.rst | 5 +++++ pythoncapi_compat.h | 15 +++++++++++++++ tests/test_pythoncapi_compat_cext.c | 28 ++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+) 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} }; pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy