Skip to content

Commit 9a953db

Browse files
Issue python#28808: PyUnicode_CompareWithASCIIString() now never raises exceptions.
2 parents 19d2467 + 419967b commit 9a953db

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

Doc/c-api/unicode.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,8 +1657,7 @@ They all return *NULL* or ``-1`` if an exception occurs.
16571657
ASCII-encoded strings, but the function interprets the input string as
16581658
ISO-8859-1 if it contains non-ASCII characters.
16591659
1660-
This function returns ``-1`` upon failure, so one should call
1661-
:c:func:`PyErr_Occurred` to check for errors.
1660+
This function does not raise exceptions.
16621661
16631662
16641663
.. c:function:: PyObject* PyUnicode_RichCompare(PyObject *left, PyObject *right, int op)

Include/unicodeobject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2051,7 +2051,7 @@ PyAPI_FUNC(int) _PyUnicode_EqualToASCIIId(
20512051
equal, and greater than, respectively. It is best to pass only
20522052
ASCII-encoded strings, but the function interprets the input string as
20532053
ISO-8859-1 if it contains non-ASCII characters.
2054-
Raise an exception and return -1 on error. */
2054+
This function does not raise exceptions. */
20552055

20562056
PyAPI_FUNC(int) PyUnicode_CompareWithASCIIString(
20572057
PyObject *left,

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ Library
3636

3737
- Issue #28843: Fix asyncio C Task to handle exceptions __traceback__.
3838

39+
C API
40+
-----
41+
42+
- Issue #28808: PyUnicode_CompareWithASCIIString() now never raises exceptions.
43+
3944
Documentation
4045
-------------
4146

Objects/unicodeobject.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11011,10 +11011,24 @@ PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str)
1101111011
Py_ssize_t i;
1101211012
int kind;
1101311013
Py_UCS4 chr;
11014+
const unsigned char *ustr = (const unsigned char *)str;
1101411015

1101511016
assert(_PyUnicode_CHECK(uni));
11016-
if (PyUnicode_READY(uni) == -1)
11017-
return -1;
11017+
if (!PyUnicode_IS_READY(uni)) {
11018+
const wchar_t *ws = _PyUnicode_WSTR(uni);
11019+
/* Compare Unicode string and source character set string */
11020+
for (i = 0; (chr = ws[i]) && ustr[i]; i++) {
11021+
if (chr != ustr[i])
11022+
return (chr < ustr[i]) ? -1 : 1;
11023+
}
11024+
/* This check keeps Python strings that end in '\0' from comparing equal
11025+
to C strings identical up to that point. */
11026+
if (_PyUnicode_WSTR_LENGTH(uni) != i || chr)
11027+
return 1; /* uni is longer */
11028+
if (ustr[i])
11029+
return -1; /* str is longer */
11030+
return 0;
11031+
}
1101811032
kind = PyUnicode_KIND(uni);
1101911033
if (kind == PyUnicode_1BYTE_KIND) {
1102011034
const void *data = PyUnicode_1BYTE_DATA(uni);

0 commit comments

Comments
 (0)