Avoid PyUnicode_AS_UNICODE in the UTF-8 encoder
authorVictor Stinner <victor.stinner@haypocalc.com>
Thu, 10 Nov 2011 19:21:49 +0000 (20:21 +0100)
committerVictor Stinner <victor.stinner@haypocalc.com>
Thu, 10 Nov 2011 19:21:49 +0000 (20:21 +0100)
Objects/unicodeobject.c

index 4c509b4b39f5b2bb017d43a802b391cda457fbff..cf1f2eae60378ddad093f8f0d71f96616dd9fff3 100644 (file)
@@ -4820,11 +4820,18 @@ _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors)
                 for(k = repsize; k > 0; k--)
                     *p++ = *prep++;
             } else /* rep is unicode */ {
-                const Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep);
-                Py_UNICODE c;
+                enum PyUnicode_Kind repkind;
+                void *repdata;
+
+                if (PyUnicode_READY(rep) < 0) {
+                    Py_DECREF(rep);
+                    goto error;
+                }
+                repkind = PyUnicode_KIND(rep);
+                repdata = PyUnicode_DATA(rep);
 
                 for(k=0; k<repsize; k++) {
-                    c = prep[k];
+                    Py_UCS4 c = PyUnicode_READ(repkind, repdata, k);
                     if (0x80 <= c) {
                         raise_encode_exception(&exc, "utf-8",
                                                unicode,
@@ -4832,7 +4839,7 @@ _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors)
                                                "surrogates not allowed");
                         goto error;
                     }
-                    *p++ = (char)prep[k];
+                    *p++ = (char)c;
                 }
             }
             Py_DECREF(rep);