10000 Add PyList_Extend() (#80) · python/pythoncapi-compat@1c1ab38 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1c1ab38

Browse files
authored
Add PyList_Extend() (#80)
1 parent 85e4cd5 commit 1c1ab38

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

docs/api.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,14 @@ Python 3.13
121121
122122
See `PyUnicode_EqualToUTF8AndSize() documentation <https://docs.python.org/dev/c-api/unicode.html#c.PyUnicode_EqualToUTF8AndSize>`__.
123123
124+
.. c:function:: int PyList_Extend(PyObject *list, PyObject *iterable)
125+
126+
See `PyList_Extend() documentation <https://docs.python.org/dev/c-api/list.html#c.PyList_Extend>`__.
127+
128+
.. c:function:: int PyList_Clear(PyObject *list)
129+
130+
See `PyList_Clear() documentation <https://docs.python.org/dev/c-api/list.html#c.PyList_Clear>`__.
131+
124132
125133
Python 3.12
126134
-----------

docs/changelog.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Changelog
22
=========
33

4+
* 2023-11-13: Add functions:
5+
6+
* ``PyList_Extend()``
7+
* ``PyList_Clear()``
8+
49
* 2023-10-04: Add functions:
510

611
* ``PyUnicode_EqualToUTF8()``

pythoncapi_compat.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,21 @@ PyUnicode_EqualToUTF8(PyObject *unicode, const char *str)
10131013
#endif
10141014

10151015

1016+
// gh-111138 added PyList_Extend() and PyList_Clear() to Python 3.13.0a2
1017+
#if PY_VERSION_HEX < 0x030D00A2
1018+
static inline int
1019+
PyList_Extend(PyObject *list, PyObject *iterable)
1020+
{
1021+
return PyList_SetSlice(list, PY_SSIZE_T_MAX, PY_SSIZE_T_MAX, iterable);
1022+
}
1023+
1024+
static inline int
1025+
PyList_Clear(PyObject *list)
1026+
{
1027+
return PyList_SetSlice(list, 0, PY_SSIZE_T_MAX, NULL);
1028+
}
1029+
#endif
1030+
10161031
#ifdef __cplusplus
10171032
}
10181033
#endif

tests/test_pythoncapi_compat_cext.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,6 +1395,33 @@ test_unicode(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
13951395
}
13961396

13971397

1398+
static PyObject *
1399+
test_list(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
1400+
{
1401+
PyObject *list = PyList_New(0);
1402+
if (list == NULL) {
1403+
return NULL;
1404+
}
1405+
1406+
PyObject *abc = PyUnicode_FromString("abc");
1407+
if (abc == NULL) {
1408+
return NULL;
1409+
}
1410+
1411+
// test PyList_Extend()
1412+
assert(PyList_Extend(list, abc) == 0);
1413+
Py_DECREF(abc);
1414+
assert(PyList_GET_SIZE(list) == 3);
1415+
1416+
// test PyList_Clear()
1417+
assert(PyList_Clear(list) == 0);
1418+
assert(PyList_GET_SIZE(list) == 0);
1419+
1420+
Py_DECREF(list);
1421+
Py_RETURN_NONE;
1422+
}
1423+
1424+
13981425
static struct PyMethodDef methods[] = {
13991426
{"test_object", test_object, METH_NOARGS, _Py_NULL},
14001427
{"test_py_is", test_py_is, METH_NOARGS, _Py_NULL},
@@ -1425,6 +1452,7 @@ static struct PyMethodDef methods[] = {
14251452
{"test_managed_dict", test_managed_dict, METH_NOARGS, _Py_NULL},
14261453
#endif
14271454
{"test_unicode", test_unicode, METH_NOARGS, _Py_NULL},
1455+
{"test_list", test_list, METH_NOARGS, _Py_NULL},
14281456
{_Py_NULL, _Py_NULL, 0, _Py_NULL}
14291457
};
14301458

0 commit comments

Comments
 (0)
0