]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-45061: Revert unicode_is_singleton() change (GH-28516)
authorVictor Stinner <vstinner@python.org>
Wed, 22 Sep 2021 10:16:53 +0000 (12:16 +0200)
committerGitHub <noreply@github.com>
Wed, 22 Sep 2021 10:16:53 +0000 (12:16 +0200)
Don't use a loop over 256 items, only checks for a single singleton.

Objects/unicodeobject.c

index f5919cf5a95cd905dcb0fa4e5051af43c0f641c7..02bf56e681e56ea358e4cfeec80543b61c6fd5c0 100644 (file)
@@ -1994,8 +1994,10 @@ unicode_is_singleton(PyObject *unicode)
     if (unicode == state->empty_string) {
         return 1;
     }
-    for (Py_ssize_t i = 0; i < 256; i++) {
-        if (unicode == state->latin1[i]) {
+    PyASCIIObject *ascii = (PyASCIIObject *)unicode;
+    if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) {
+        Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0);
+        if (ch < 256 && state->latin1[ch] == unicode) {
             return 1;
         }
     }