diff Objects/unicodeobject.c @ 73482:7474bbee98cd

Avoid PyUnicode_AS_UNICODE in the UTF-8 encoder
author Victor Stinner <victor.stinner@haypocalc.com>
date Thu, 10 Nov 2011 20:21:49 +0100
parents 5a9248c32b87
children 8797854b9c38
line wrap: on
line diff
--- a/Objects/unicodeobject.c	Thu Nov 10 20:15:25 2011 +0100
+++ b/Objects/unicodeobject.c	Thu Nov 10 20:21:49 2011 +0100
@@ -4820,11 +4820,18 @@
                 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 @@
                                                "surrogates not allowed");
                         goto error;
                     }
-                    *p++ = (char)prep[k];
+                    *p++ = (char)c;
                 }
             }
             Py_DECREF(rep);