[Python-checkins] bpo-45061: Revert unicode_is_singleton() change (GH-28516)

vstinner webhook-mailer at python.org
Wed Sep 22 06:16:58 EDT 2021


https://github.com/python/cpython/commit/8620be99da930230b18ec05f4d7446ee403531af
commit: 8620be99da930230b18ec05f4d7446ee403531af
branch: main
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2021-09-22T12:16:53+02:00
summary:

bpo-45061: Revert unicode_is_singleton() change (GH-28516)

Don't use a loop over 256 items, only checks for a single singleton.

files:
M Objects/unicodeobject.c

diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index f5919cf5a95cd..02bf56e681e56 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -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;
         }
     }



More information about the Python-checkins mailing list