Skip to content

Commit

Permalink
Add PyList_Extend()
Browse files Browse the repository at this point in the history
  • Loading branch information
vstinner committed Nov 13, 2023
1 parent 85e4cd5 commit 6bfb71b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
8 changes: 8 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ Python 3.13
See `PyUnicode_EqualToUTF8AndSize() documentation <https://docs.python.org/dev/c-api/unicode.html#c.PyUnicode_EqualToUTF8AndSize>`__.
.. c:function:: int PyList_Extend(PyObject *list, PyObject *iterable)
See `PyList_Extend() documentation <https://docs.python.org/dev/c-api/list.html#c.PyList_Extend>`__.
.. c:function:: int PyList_Clear(PyObject *list)
See `PyList_Clear() documentation <https://docs.python.org/dev/c-api/list.html#c.PyList_Clear>`__.
Python 3.12
-----------
Expand Down
5 changes: 5 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

* 2023-11-13: Add functions:

* ``PyList_Extend()``
* ``PyList_Clear()``

* 2023-10-04: Add functions:

* ``PyUnicode_EqualToUTF8()``
Expand Down
15 changes: 15 additions & 0 deletions pythoncapi_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions tests/test_pythoncapi_compat_cext.c
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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}
};

Expand Down

0 comments on commit 6bfb71b

Please sign in to comment.