Skip to content

Commit

Permalink
Add PyBytes_Join() function (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
vstinner authored Oct 9, 2024
1 parent abc0f29 commit fba4977
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ Python 3.14
See `PyLong_GetSign() documentation <https://docs.python.org/dev/c-api/long.html#c.PyLong_GetSign>`__.
.. c:function:: PyObject* PyBytes_Join(PyObject *sep, PyObject *iterable)
See `PyBytes_Join() documentation <https://docs.python.org/dev/c-api/bytes.html#c.PyBytes_Join>`__.
.. c:function:: int PyUnicode_Equal(PyObject *str1, PyObject *str2)
See `PyUnicode_Equal() documentation <https://docs.python.org/dev/c-api/unicode.html#c.PyUnicode_Equal>`__.
Expand Down
6 changes: 5 additions & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
Changelog
=========

* 2024-10-09: Add ``PyUnicode_Equal()`` function.
* 2024-10-09: Add functions:

* ``PyBytes_Join()``
* ``PyUnicode_Equal()``

* 2024-07-18: Add functions:

* ``PyUnicodeWriter_Create()``
Expand Down
9 changes: 9 additions & 0 deletions pythoncapi_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -1546,6 +1546,15 @@ static inline int PyUnicode_Equal(PyObject *str1, PyObject *str2)
#endif


// gh-121645 added PyBytes_Join() to Python 3.14.0a0
#if PY_VERSION_HEX < 0x030E00A0
static inline PyObject* PyBytes_Join(PyObject *sep, PyObject *iterable)
{
return _PyBytes_Join(sep, iterable);
}
#endif


#ifdef __cplusplus
}
#endif
Expand Down
31 changes: 31 additions & 0 deletions tests/test_pythoncapi_compat_cext.c
Original file line number Diff line number Diff line change
Expand Up @@ -1881,6 +1881,36 @@ test_unicodewriter_format(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args))
#endif


static PyObject *
test_bytes(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
{
PyObject *abc = PyBytes_FromString("a b c");
if (abc == NULL) {
return NULL;
}
PyObject *list = PyObject_CallMethod(abc, "split", NULL);
Py_DECREF(abc);
if (list == NULL) {
return NULL;
}
PyObject *sep = PyBytes_FromString("-");
if (sep == NULL) {
Py_DECREF(list);
return NULL;
}

PyObject *join = PyBytes_Join(sep, list);
assert(join != NULL);
assert(PyBytes_Check(join));
assert(memcmp(PyBytes_AS_STRING(join), "a-b-c", 5) == 0);
Py_DECREF(join);

Py_DECREF(list);
Py_DECREF(sep);
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 @@ -1924,6 +1954,7 @@ static struct PyMethodDef methods[] = {
{"test_unicodewriter_widechar", test_unicodewriter_widechar, METH_NOARGS, _Py_NULL},
{"test_unicodewriter_format", test_unicodewriter_format, METH_NOARGS, _Py_NULL},
#endif
{"test_bytes", test_bytes, METH_NOARGS, _Py_NULL},
{_Py_NULL, _Py_NULL, 0, _Py_NULL}
};

Expand Down

0 comments on commit fba4977

Please sign in to comment.