diff options
author | Victor Stinner <victor.stinner@haypocalc.com> | 2011-11-16 23:45:54 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@haypocalc.com> | 2011-11-16 23:45:54 (GMT) |
commit | 1f7951711c16cca5f041288b81d01cb3021d0b7e (patch) | |
tree | c7bd5aed6f0eaace7b8180c1bc3c5fa76f958578 /Objects | |
parent | 6f9568bb1faa986f547d541817bc3e0dec708127 (diff) | |
download | cpython-1f7951711c16cca5f041288b81d01cb3021d0b7e.zip cpython-1f7951711c16cca5f041288b81d01cb3021d0b7e.tar.gz cpython-1f7951711c16cca5f041288b81d01cb3021d0b7e.tar.bz2 |
Catch PyUnicode_AS_UNICODE() errors
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/unicodeobject.c | 33 |
1 files changed, 22 insertions, 11 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index a0459a0..b986193 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -3051,9 +3051,13 @@ PyObject * PyUnicode_EncodeFSDefault(PyObject *unicode) { #ifdef HAVE_MBCS - return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), - PyUnicode_GET_SIZE(unicode), - NULL); + const Py_UNICODE *wstr; + Py_ssize_t wlen; + + wstr = PyUnicode_AsUnicodeAndSize(unicode, &wlen); + if (wstr == NULL) + return NULL; + return PyUnicode_EncodeMBCS(wstr, wlen, NULL); #elif defined(__APPLE__) return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); #else @@ -3137,10 +3141,15 @@ PyUnicode_AsEncodedString(PyObject *unicode, (strcmp(lower, "iso-8859-1") == 0)) return _PyUnicode_AsLatin1String(unicode, errors); #ifdef HAVE_MBCS - else if (strcmp(lower, "mbcs") == 0) - return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), - PyUnicode_GET_SIZE(unicode), - errors); + else if (strcmp(lower, "mbcs") == 0) { + const Py_UNICODE *wstr; + Py_ssize_t wlen; + + wstr = PyUnicode_AsUnicodeAndSize(unicode, &wlen); + if (wstr == NULL) + return NULL; + return PyUnicode_EncodeMBCS(wstr, wlen, errors); + } #endif else if (strcmp(lower, "ascii") == 0) return _PyUnicode_AsASCIIString(unicode, errors); @@ -5148,10 +5157,12 @@ PyUnicode_EncodeUTF32(const Py_UNICODE *s, PyObject * PyUnicode_AsUTF32String(PyObject *unicode) { - return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), - PyUnicode_GET_SIZE(unicode), - NULL, - 0); + const Py_UNICODE *wstr; + Py_ssize_t wlen; + wstr = PyUnicode_AsUnicodeAndSize(unicode, &wlen); + if (wstr == NULL) + return NULL; + return PyUnicode_EncodeUTF32(wstr, wlen, NULL, 0); } /* --- UTF-16 Codec ------------------------------------------------------- */ |