Skip to content

Commit 8953192

Browse files
committed
Address reviews
1 parent 4a2ae3d commit 8953192

File tree

5 files changed

+25
-17
lines changed

5 files changed

+25
-17
lines changed

Doc/c-api/unicode.rst

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,7 +1423,9 @@ They all return ``NULL`` or ``-1`` if an exception occurs.
14231423
This function returns ``-1`` upon failure, so one should call
14241424
:c:func:`PyErr_Occurred` to check for errors.
14251425
1426-
See also :c:func:`PyUnicode_Equal`.
1426+
.. seealso::
1427+
1428+
The :c:func:`PyUnicode_Equal` function.
14271429
14281430
14291431
.. c:function:: int PyUnicode_Equal(PyObject *a, PyObject *b)
@@ -1433,11 +1435,13 @@ They all return ``NULL`` or ``-1`` if an exception occurs.
14331435
* Return ``1`` if *a* is equal to *b*.
14341436
* Return ``0`` if *a* is not equal to *b*.
14351437
* Set a :exc:`TypeError` exception and return ``-1`` if *a* or *b* is not a
1436-
Python :class:`str` object.
1438+
:class:`str` object.
14371439
1438-
The function always succeed if *a* and *b* are Python :class:`str` objects.
1440+
The function always succeeds if *a* and *b* are :class:`str` objects.
1441+
1442+
.. seealso::
14391443
1440-
See also :c:func:`PyUnicode_Compare`.
1444+
The :c:func:`PyUnicode_Compare` function.
14411445
14421446
.. versionadded:: 3.14
14431447

Doc/whatsnew/3.14.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,8 @@ New Features
646646
<https://peps.python.org/pep-0630/#type-checking>`__ mentioned in :pep:`630`
647647
(:gh:`124153`).
648648

649-
* Add :c:func:`PyUnicode_Equal` function to test if two strings are equal.
649+
* Add :c:func:`PyUnicode_Equal` function to the limited C API:
650+
test if two strings are equal.
650651
(Contributed by Victor Stinner in :gh:`124502`.)
651652

652653

Lib/test/test_capi/test_unicode.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1919,6 +1919,12 @@ def copy(text):
19191919
self.assertFalse(unicode_equal("\u20ac", "\u20ad"))
19201920
self.assertFalse(unicode_equal("\U0010ffff", "\U0010fffe"))
19211921

1922+
# str subclass
1923+
self.assertTrue(unicode_equal("abc", Str("abc")))
1924+
self.assertTrue(unicode_equal(Str("abc"), "abc"))
1925+
self.assertFalse(unicode_equal("abc", Str("abcd")))
1926+
self.assertFalse(unicode_equal(Str("abc"), "abcd"))
1927+
19221928
# invalid type
19231929
for invalid_type in (b'bytes', 123, ("tuple",)):
19241930
with self.subTest(invalid_type=invalid_type):
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Add :c:func:`PyUnicode_Equal` function to test if two strings are equal.
2-
Patch by Victor Stinner.
1+
Add :c:func:`PyUnicode_Equal` function to the limited C API: test if two
2+
strings are equal. Patch by Victor Stinner.

Objects/unicodeobject.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11012,17 +11012,14 @@ _PyUnicode_Equal(PyObject *str1, PyObject *str2)
1101211012
int
1101311013
PyUnicode_Equal(PyObject *str1, PyObject *str2)
1101411014
{
11015-
if (!PyUnicode_Check(str1)) {
11016-
PyErr_Format(PyExc_TypeError,
11017-
"first argument must be str, not %T", str1);
11018-
return -1;
11019-
}
11020-
if (!PyUnicode_Check(str2)) {
11021-
PyErr_Format(PyExc_TypeError,
11022-
"second argument must be str, not %T", str2);
11023-
return -1;
11015+
if (PyUnicode_Check(str1) && PyUnicode_Check(str2)) {
11016+
return _PyUnicode_Equal(str1, str2);
1102411017
}
11025-
return _PyUnicode_Equal(str1, str2);
11018+
11019+
PyErr_Format(PyExc_TypeError,
11020+
"Can't compare %T and %T",
11021+
str1, str2);
11022+
return -1;
1102611023
}
1102711024

1102811025

0 commit comments

Comments
 (0)