Index: Objects/unicodeobject.c =================================================================== --- Objects/unicodeobject.c (révision 81858) +++ Objects/unicodeobject.c (copie de travail) @@ -1488,11 +1488,15 @@ encoding = PyUnicode_GetDefaultEncoding(); /* Shortcuts for common default encodings */ - if (strcmp(encoding, "utf-8") == 0) - return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), - PyUnicode_GET_SIZE(unicode), - errors); - else if (strcmp(encoding, "latin-1") == 0) + if (strcmp(encoding, "utf-8") == 0) { + if (errors == NULL || strcmp(errors, "strict") == 0) { + return PyUnicode_AsUTF8String(unicode); + } else { + return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), + PyUnicode_GET_SIZE(unicode), + errors); + } + } else if (strcmp(encoding, "latin-1") == 0) return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), errors); @@ -2677,13 +2681,16 @@ PyObject *PyUnicode_AsUTF8String(PyObject *unicode) { + PyObject *utf8; if (!PyUnicode_Check(unicode)) { PyErr_BadArgument(); return NULL; } - return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), - PyUnicode_GET_SIZE(unicode), - NULL); + utf8 = _PyUnicode_AsDefaultEncodedString(unicode, NULL); + if (utf8 == NULL) + return NULL; + Py_INCREF(utf8); + return utf8; } /* --- UTF-32 Codec ------------------------------------------------------- */