summaryrefslogtreecommitdiffstats
path: root/Objects/unicodeobject.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2016-12-05 22:17:45 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2016-12-05 22:17:45 (GMT)
commit9a953dbb34b722dc16e98ef7103eb49b72a04e5e (patch)
treeec4dbef68599b6502ca6b425ad036c6d8d02f405 /Objects/unicodeobject.c
parent19d246745d9d013c12e9560dd020d778381780fb (diff)
parent419967b8320be40d7948bde042e92b6e7d1f48a7 (diff)
downloadcpython-9a953dbb34b722dc16e98ef7103eb49b72a04e5e.zip
cpython-9a953dbb34b722dc16e98ef7103eb49b72a04e5e.tar.gz
cpython-9a953dbb34b722dc16e98ef7103eb49b72a04e5e.tar.bz2
Issue #28808: PyUnicode_CompareWithASCIIString() now never raises exceptions.
Diffstat (limited to 'Objects/unicodeobject.c')
-rw-r--r--Objects/unicodeobject.c18
1 files changed, 16 insertions, 2 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 2cd9cbf..9c998f7 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);