8000 Add PyBytes_Join() function (#111) · python/pythoncapi-compat@fba4977 · GitHub
[go: up one dir, main page]

Skip to content

Commit fba4977

Browse files
authored
Add PyBytes_Join() function (#111)
1 parent abc0f29 commit fba4977

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

docs/api.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ Python 3.14
3333
3434
See `PyLong_GetSign() documentation <https://docs.python.org/dev/c-api/long.html#c.PyLong_GetSign>`__.
3535
36+
.. c:function:: PyObject* PyBytes_Join(PyObject *sep, PyObject *iterable)
37+
38+
See `PyBytes_Join() documentation <https://docs.python.org/dev/c-api/bytes.html#c.PyBytes_Join>`__.
39+
3640
.. c:function:: int PyUnicode_Equal(PyObject *str1, PyObject *str2)
3741
3842
See `PyUnicode_Equal() documentation <https://docs.python.org/dev/c-api/unicode.html#c.PyUnicode_Equal>`__.

docs/changelog.rst

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

4-
* 2024-10-09: Add ``PyUnicode_Equal()`` function.
4+
* 2024-10-09: Add functions:
5+
6+
* ``PyBytes_Join()``
7+
* ``PyUnicode_Equal()``
8+
59
* 2024-07-18: Add functions:
610

711
* ``PyUnicodeWriter_Create()``

pythoncapi_compat.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,6 +1546,15 @@ static inline int PyUnicode_Equal(PyObject *str1, PyObject *str2)
15461546
#endif
15471547

15481548

1549+
// gh-121645 added PyBytes_Join() to Python 3.14.0a0
1550+
#if PY_VERSION_HEX < 0x030E00A0
1551+
static inline PyObject* PyBytes_Join(PyObject *sep, PyObject *iterable)
1552+
{
1553+
return _PyBytes_Join(sep, iterable);
1554+
}
1555+
#endif
1556+
1557+
15491558
#ifdef __cplusplus
15501559
}
15511560
#endif

tests/test_pythoncapi_compat_cext.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,6 +1881,36 @@ test_unicodewriter_format(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args))
18811881
#endif
18821882

18831883

1884+
static PyObject *
1885+
test_bytes(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
1886+
{
1887+
PyObject *abc = PyBytes_FromString("a b c");
1888+
if (abc == NULL) {
1889+
return NULL;
1890+
}
1891+
PyObject *list = PyObject_CallMethod(abc, "split", NULL);
1892+
Py_DECREF(abc);
1893+
if (list == NULL) {
1894+
return NULL;
1895+
}
1896+
PyObject *sep = PyBytes_FromString("-");
1897+
if (sep == NULL) {
1898+
Py_DECREF(list);
1899+
return NULL;
1900+
}
1901+
1902+
PyObject *join = PyBytes_Join(sep, list);
1903+
assert(join != NULL);
1904+
assert(PyBytes_Check(join));
1905+
assert(memcmp(PyBytes_AS_STRING(join), "a-b-c", 5) == 0);
1906+
Py_DECREF(join);
1907+
1908+
Py_DECREF(list);
1909+
Py_DECREF(sep);
1910+
Py_RETURN_NONE;
1911+
}
1912+
1913+
18841914
static struct PyMethodDef methods[] = {
18851915
{"test_object", test_object, METH_NOARGS, _Py_NULL},
18861916
{"test_py_is", test_py_is, METH_NOARGS, _Py_NULL},
@@ -1924,6 +1954,7 @@ static struct PyMethodDef methods[] = {
19241954
{"test_unicodewriter_widechar", test_unicodewriter_widechar, METH_NOARGS, _Py_NULL},
19251955
{"test_unicodewriter_format", test_unicodewriter_format, METH_NOARGS, _Py_NULL},
19261956
#endif
1957+
{"test_bytes", test_bytes, METH_NOARGS, _Py_NULL},
19271958
{_Py_NULL, _Py_NULL, 0, _Py_NULL}
19281959
};
19291960

0 commit comments

Comments
 (0)
0