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

Skip to content

Add PyBytes_Join() function #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(PyObjec 8000 t *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