Index: Objects/unicodeobject.c =================================================================== --- Objects/unicodeobject.c (revision 58574) +++ Objects/unicodeobject.c (working copy) @@ -1056,21 +1056,39 @@ { PyObject *buffer = NULL, *unicode; Py_buffer info; + /* strlen("latin-1") + 1 safety + \0 */ + char lower[9], *l = lower, *e; if (encoding == NULL) - encoding = PyUnicode_GetDefaultEncoding(); + encoding = PyUnicode_GetDefaultEncoding(); + /* Convert encoding to lower case in order to catch e.g. UTF-8 */ + e = (char*)encoding; + while (*e && (l - lower) < 8) { + if (isupper(Py_CHARMASK(*e))) { + *l++ = tolower(Py_CHARMASK(*e++)); + } + else { + *l++ = *e++; + } + } + *l = '\0'; + /* Shortcuts for common default encodings */ - if (strcmp(encoding, "utf-8") == 0) + if (strcmp(lower, "utf-8") == 0) return PyUnicode_DecodeUTF8(s, size, errors); - else if (strcmp(encoding, "latin-1") == 0) + else if (strcmp(lower, "latin-1") == 0) return PyUnicode_DecodeLatin1(s, size, errors); #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) - else if (strcmp(encoding, "mbcs") == 0) + else if (strcmp(lower, "mbcs") == 0) return PyUnicode_DecodeMBCS(s, size, errors); #endif - else if (strcmp(encoding, "ascii") == 0) + else if (strcmp(lower, "ascii") == 0) return PyUnicode_DecodeASCII(s, size, errors); + else if (strcmp(lower, "utf-16") == 0) + return PyUnicode_DecodeUTF16(s, size, errors, 0); + else if (strcmp(lower, "utf-32") == 0) + return PyUnicode_DecodeUTF32(s, size, errors, 0); /* Decode via the codec registry */ buffer = NULL; Index: Lib/test/regrtest.py =================================================================== --- Lib/test/regrtest.py (revision 58574) +++ Lib/test/regrtest.py (working copy) @@ -1119,6 +1119,15 @@ if not os.path.supports_unicode_filenames: self.expected.add('test_pep277') + # doctest, profile and cProfile tests fail when the codec for the fs + # encoding isn't built in because PyUnicode_Decode() adds two calls + # into Python. + encs = ("utf-8", "latin-1", "ascii", "mbcs", "utf-16", "utf-32") + if sys.getfilesystemencoding().lower() not in encs: + self.expected.add('test_profile') + self.expected.add('test_cProfile') + self.expected.add('test_doctest') + try: from test import test_socket_ssl except ImportError: