Skip to content

Commit 6d02285

Browse files
committed
Add PyUnicode_Equal() function
1 parent bb0934e commit 6d02285

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

docs/api.rst

+4
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

+1
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

+30
Original file line numberDiff line numberDiff line change
@@ -1514,6 +1514,36 @@ 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
1535+
extern int _PyUnicode_Equal(PyObject *str1, PyObject *str2);
1536+
1537+
return _PyUnicode_Equal(str1, str2);
1538+
#elif PY_VERSION_HEX >= 0x03060000
1539+
return _PyUnicode_EQ(str1, str2);
1540+
#else
1541+
return (PyUnicode_Compare(str1, str2) == 0);
1542+
#endif
1543+
}
1544+
#endif
1545+
1546+
15171547
#ifdef __cplusplus
15181548
}
15191549
#endif

tests/test_pythoncapi_compat_cext.c

+7
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)