-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
gh-110289: C API: Add PyUnicode_EqualToUTF8() function #110297
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
Changes from 1 commit
d39945e
4793161
8b24911
c55f9ac
bdf2f1e
7223c14
b271327
6f26ad6
dd124b8
76b9177
ee5781d
1a4eb7b
b124377
029f1a0
be2ffe8
29b26f7
78de49d
fc79d5e
19ad126
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
8000
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -1001,7 +1001,7 @@ New Features | |||||||||
:c:macro:`Py_TPFLAGS_MANAGED_DICT` flag. | ||||||||||
(Contributed by Victor Stinner in :gh:`107073`.) | ||||||||||
|
||||||||||
* Add :c:func:`PyUnicode_EqualToString` function: compare Unicode object with | ||||||||||
* Add :c:func:`PyUnicode_EqualToUTF8` function: compare Unicode object with | ||||||||||
a :c:expr:`const char*` UTF-8 encoded bytes string and return true if they | ||||||||||
are equal or false otherwise. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
This function does not raise exceptions. | ||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1297,6 +1297,37 @@ def test_comparewithasciistring(self): | |||||
# CRASHES comparewithasciistring([], b'abc') | ||||||
# CRASHES comparewithasciistring(NULL, b'abc') | ||||||
|
||||||
@support.cpython_only | ||||||
@unittest.skipIf(_testcapi is None, 'need _testcapi module') | ||||||
def test_equaltoutf8(self): | ||||||
"""Test PyUnicode_EqualToUTF8()""" | ||||||
erlend-aasland marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
from _testcapi import unicode_equaltoutf8 as equaltoutf8 | ||||||
|
||||||
strings = [ | ||||||
'abc', '\xa1\xa2\xa3', '\u4f60\u597d\u4e16', | ||||||
'\U0001f600\U0001f601\U0001f602' | ||||||
serhiy-storchaka marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
] | ||||||
serhiy-storchaka marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
for s in strings: | ||||||
b = s.encode() | ||||||
self.assertEqual(equaltoutf8(s, b), 1) | ||||||
self.assertEqual(equaltoutf8(b.decode(), b), 1) | ||||||
self.assertEqual(equaltoutf8(s + 'x', b + b'x'), 1) | ||||||
self.assertEqual(equaltoutf8(s + 'x', b + b'y'), 0) | ||||||
self.assertEqual(equaltoutf8(s + '\0', b + b'\0'), 0) | ||||||
self.assertEqual(equaltoutf8(s, b + b'x'), 0) | ||||||
self.assertEqual(equaltoutf8(s, b[:-1]), 0) | ||||||
self.assertEqual(equaltoutf8(s, b[:-1] + b'x'), 0) | ||||||
|
||||||
# surrogateescape | ||||||
serhiy-storchaka marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
self.assertEqual(equaltoutf8('\udcfe', b'\xfe'), 0) | ||||||
serhiy-storchaka marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
# surrogatepass | ||||||
self.assertEqual(equaltoutf8('\udcfe', b'\xed\xb3\xbe'), 0) | ||||||
serhiy-storchaka marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
# CRASHES equaltoutf8(b'abc', b'abc') | ||||||
# CRASHES equaltoutf8([], b'abc') | ||||||
# CRASHES equaltoutf8(NULL, b'abc') | ||||||
# CRASHES equaltoutf8('abc') # NULL | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, it does not work so. NULL is defined as None, and If There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh ok, I thought that they were just pseudo-code as comments. Sure, you can leave There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I copied this pattern from the test for |
||||||
|
||||||
@support.cpython_only | ||||||
@unittest.skipIf(_testcapi is None, 'need _testcapi module') | ||||||
def test_richcompare(self): | ||||||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
Add :c:func:`PyUnicode_EqualToString` function. | ||
Add :c:func:`PyUnicode_EqualToUTF8` function. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks to me, that "return true" is more often used than "return non-zero". In this case it is more accurate, because it always returns 1, not other non-zero value. Perhaps other functions which return non-zero was a macro that returned not 1 (something like
(arg->flags & FLAG)
)?