diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2016-12-05 22:20:26 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2016-12-05 22:20:26 (GMT) |
commit | fb3134f4d489556ff3cb8fcf7e9948bdab2e49d8 (patch) | |
tree | 921dcd951db59be29636959f8bda74cf63b0c111 /Objects/unicodeobject.c | |
parent | 9083eb6aac6e0aa0eb1dee9bc535538de4125fcd (diff) | |
parent | 9a953dbb34b722dc16e98ef7103eb49b72a04e5e (diff) | |
download | cpython-fb3134f4d489556ff3cb8fcf7e9948bdab2e49d8.zip cpython-fb3134f4d489556ff3cb8fcf7e9948bdab2e49d8.tar.gz cpython-fb3134f4d489556ff3cb8fcf7e9948bdab2e49d8.tar.bz2 |
Issue #28808: PyUnicode_CompareWithASCIIString() now never raises exceptions.
Diffstat (limited to 'Objects/unicodeobject.c')
-rw-r--r-- | Objects/unicodeobject.c | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 1c2257e..7f58129 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -10977,10 +10977,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); |