summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2016-12-06 00:17:45 +0200
committerSerhiy Storchaka <storchaka@gmail.com>2016-12-06 00:17:45 +0200
commitc6e49d6743e00989891c03edf2f0a76592169b9d (patch)
tree5a68409a00700287fa6b14b9b148e48dccbb38b9
parentf1b02866add58317c7f9c6c813c282659981a147 (diff)
parented3753ef8d1fc136265ba52163a484f67dfb7ecc (diff)
downloadcpython-c6e49d6743e00989891c03edf2f0a76592169b9d.tar.gz
Issue #28808: PyUnicode_CompareWithASCIIString() now never raises exceptions.
-rw-r--r--Doc/c-api/unicode.rst3
-rw-r--r--Include/unicodeobject.h2
-rw-r--r--Misc/NEWS5
-rw-r--r--Objects/unicodeobject.c18
4 files changed, 23 insertions, 5 deletions
diff --git a/Doc/c-api/unicode.rst b/Doc/c-api/unicode.rst
index fc91d0cac5..c11e049fea 100644
--- a/Doc/c-api/unicode.rst
+++ b/Doc/c-api/unicode.rst
@@ -1657,8 +1657,7 @@ They all return *NULL* or ``-1`` if an exception occurs.
ASCII-encoded strings, but the function interprets the input string as
ISO-8859-1 if it contains non-ASCII characters.
- This function returns ``-1`` upon failure, so one should call
- :c:func:`PyErr_Occurred` to check for errors.
+ This function does not raise exceptions.
.. c:function:: PyObject* PyUnicode_RichCompare(PyObject *left, PyObject *right, int op)
diff --git a/Include/unicodeobject.h b/Include/unicodeobject.h
index 2b65d197fc..8103a63ec6 100644
--- a/Include/unicodeobject.h
+++ b/Include/unicodeobject.h
@@ -2051,7 +2051,7 @@ PyAPI_FUNC(int) _PyUnicode_EqualToASCIIId(
equal, and greater than, respectively. It is best to pass only
ASCII-encoded strings, but the function interprets the input string as
ISO-8859-1 if it contains non-ASCII characters.
- Raise an exception and return -1 on error. */
+ This function does not raise exceptions. */
PyAPI_FUNC(int) PyUnicode_CompareWithASCIIString(
PyObject *left,
diff --git a/Misc/NEWS b/Misc/NEWS
index e97cd6e657..a29e50af41 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -36,6 +36,11 @@ Library
- Issue #28843: Fix asyncio C Task to handle exceptions __traceback__.
+C API
+-----
+
+- Issue #28808: PyUnicode_CompareWithASCIIString() now never raises exceptions.
+
Documentation
-------------
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 2cd9cbfa00..9c998f7ab3 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -11011,10 +11011,24 @@ PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str)
Py_ssize_t i;
int kind;
Py_UCS4 chr;
+ const unsigned char *ustr = (const unsigned char *)str;
assert(_PyUnicode_CHECK(uni));
- if (PyUnicode_READY(uni) == -1)
- return -1;
+ if (!PyUnicode_IS_READY(uni)) {
+ const wchar_t *ws = _PyUnicode_WSTR(uni);
+ /* Compare Unicode string and source character set string */
+ for (i = 0; (chr = ws[i]) && ustr[i]; i++) {
+ if (chr != ustr[i])
+ return (chr < ustr[i]) ? -1 : 1;
+ }
+ /* This check keeps Python strings that end in '\0' from comparing equal
+ to C strings identical up to that point. */
+ if (_PyUnicode_WSTR_LENGTH(uni) != i || chr)
+ return 1; /* uni is longer */
+ if (ustr[i])
+ return -1; /* str is longer */
+ return 0;
+ }
kind = PyUnicode_KIND(uni);
if (kind == PyUnicode_1BYTE_KIND) {
const void *data = PyUnicode_1BYTE_DATA(uni);