Skip to content

Commit abc0f29

Browse files
authored
Add PyUnicode_Equal() function (#110)
1 parent bb0934e commit abc0f29

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
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:: int PyUnicode_Equal(PyObject *str1, PyObject *str2)
37+
38+
See `PyUnicode_Equal() documentation <https://docs.python.org/dev/c-api/unicode.html#c.PyUnicode_Equal>`__.
39+
3640
.. c:function:: PyUnicodeWriter* PyUnicodeWriter_Create(Py_ssize_t length)
3741
3842
See `PyUnicodeWriter_Create() documentation <https://docs.python.org/dev/c-api/unicode.html#c.PyUnicodeWriter_Create>`__.

docs/changelog.rst

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

4+
* 2024-10-09: Add ``PyUnicode_Equal()`` function.
45
* 2024-07-18: Add functions:
56

67
* ``PyUnicodeWriter_Create()``

pythoncapi_compat.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,6 +1514,38 @@ static inline int PyLong_GetSign(PyObject *obj, int *sign)
15141514
#endif
15151515

15161516

1517+
// gh-124502 added PyUnicode_Equal() to Python 3.14.0a0
1518+
#if PY_VERSION_HEX < 0x030E00A0
1519+
static inline int PyUnicode_Equal(PyObject *str1, PyObject *str2)
1520+
{
1521+
if (!PyUnicode_Check(str1)) {
1522+
PyErr_Format(PyExc_TypeError,
1523+
"first argument must be str, not %s",
1524+
Py_TYPE(str1)->tp_name);
1525+
return -1;
1526+
}
1527+
if (!PyUnicode_Check(str2)) {
1528+
PyErr_Format(PyExc_TypeError,
1529+
"second argument must be str, not %s",
1530+
Py_TYPE(str2)->tp_name);
1531+
return -1;
1532+
}
1533+
1534+
#if PY_VERSION_HEX >= 0x030d0000 && !defined(PYPY_VERSION)
1535+
extern int _PyUnicode_Equal(PyObject *str1, PyObject *str2);
1536+
1537+
return _PyUnicode_Equal(str1, str2);
1538+
#elif PY_VERSION_HEX >= 0x03060000 && !defined(PYPY_VERSION)
1539+
return _PyUnicode_EQ(str1, str2);
1540+
#elif PY_VERSION_HEX >= 0x03090000 && defined(PYPY_VERSION)
1541+
return _PyUnicode_EQ(str1, str2);
1542+
#else
1543+
return (PyUnicode_Compare(str1, str2) == 0);
1544+
#endif
1545+
}
1546+
#endif
1547+
1548+
15171549
#ifdef __cplusplus
15181550
}
15191551
#endif

tests/test_pythoncapi_compat_cext.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,6 +1531,13 @@ test_unicode(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
15311531
assert(PyErr_ExceptionMatches(PyExc_MemoryError));
15321532
PyErr_Clear();
15331533

1534+
// Test PyUnicode_Equal()
1535+
assert(PyUnicode_Equal(abc, abc) == 1);
1536+
assert(PyUnicode_Equal(abc, abc0def) == 0);
1537+
assert(PyUnicode_Equal(abc, Py_True) == -1);
1538+
assert(PyErr_ExceptionMatches(PyExc_TypeError));
1539+
PyErr_Clear();
1540+
15341541
Py_DECREF(abc);
15351542
Py_DECREF(abc0def);
15361543
Py_RETURN_NONE;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy