Skip to content

Commit 44f96df

Browse files
Do not fail if iconv() cannot decode what mbstowcs() can
iconv() can be stricter than mbstowcs(), and a broken locale should not make the conversion fail, so fall back to mbstowcs() instead of raising UnicodeDecodeError. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 56ee379 commit 44f96df

1 file changed

Lines changed: 4 additions & 7 deletions

File tree

Python/fileutils.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -541,15 +541,12 @@ decode_locale_iconv(const char *arg, wchar_t **wstr, size_t *wlen,
541541
return _Py_ICONV_FALLBACK;
542542
}
543543
if (!surrogateescape) {
544+
/* Leave it to mbstowcs() to decide whether the input is
545+
valid: it can be more permissive than iconv(), and a
546+
broken locale should not make the conversion fail. */
544547
PyMem_RawFree(res);
545548
iconv_close(cd);
546-
if (wlen != NULL) {
547-
*wlen = in - arg;
548-
}
549-
if (reason != NULL) {
550-
*reason = "decoding error";
551-
}
552-
return -2;
549+
return _Py_ICONV_FALLBACK;
553550
}
554551
/* Escape the byte as UTF-8b, and start over in the initial
555552
shift state. */

0 commit comments

Comments
 (0)