diff options
author | Victor Stinner <victor.stinner@haypocalc.com> | 2011-11-10 19:21:49 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@haypocalc.com> | 2011-11-10 19:21:49 (GMT) |
commit | a98b28c1bf1fd714d7f12090154a9522c94afcc3 (patch) | |
tree | 86ff3be2363ff835ed25a109aaa4ad808f119cfc | |
parent | 3326cb6a36c6f2545d19d4cc29d5225223c2a3a2 (diff) | |
download | cpython-a98b28c1bf1fd714d7f12090154a9522c94afcc3.zip cpython-a98b28c1bf1fd714d7f12090154a9522c94afcc3.tar.gz cpython-a98b28c1bf1fd714d7f12090154a9522c94afcc3.tar.bz2 |
Avoid PyUnicode_AS_UNICODE in the UTF-8 encoder
-rw-r--r-- | Objects/unicodeobject.c | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 4c509b4..cf1f2ea 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -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); |