gh-154682: Fix corruption of locale-encoded text on FreeBSD, NetBSD, DragonFly BSD and macOS#154722
Draft
serhiy-storchaka wants to merge 4 commits into
Draft
gh-154682: Fix corruption of locale-encoded text on FreeBSD, NetBSD, DragonFly BSD and macOS#154722serhiy-storchaka wants to merge 4 commits into
serhiy-storchaka wants to merge 4 commits into
Conversation
serhiy-storchaka
requested review from
StanFromIreland and
pganssle
as code owners
July 26, 2026 09:30
…tBSD, DragonFly BSD and macOS On these platforms wchar_t values are not Unicode code points in non-UTF-8 locales, so mbstowcs() and wcstombs() cannot be used to convert locale-encoded text, neither can wcsftime() output be used, and wcscoll(), wcsxfrm() and the wide character curses API cannot be used with Unicode wchar_t. Use iconv() in _Py_DecodeLocaleEx() and _Py_EncodeLocaleEx(), use strftime() instead of wcsftime() in time.strftime(), use strcoll() and strxfrm() on the locale-encoded strings in the locale module, and convert between Unicode and the native wchar_t form on the curses library boundary. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
serhiy-storchaka
force-pushed
the
gh-154682-bsd-locale-iconv
branch
from
July 26, 2026 11:28
ef44c79 to
56ee379
Compare
serhiy-storchaka
marked this pull request as draft
July 26, 2026 12:26
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>
dotsep_as_utf8() used mbstowcs(), which does not produce Unicode code points in non-UTF-8 locales on the affected platforms. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Add _Py_LocaleNeedsWcharConversion(), which memoizes the check of the locale encoding, and test it before scanning the string. Also keep using wcscoll() and wcsxfrm() when no conversion is needed, so that locale.strxfrm() returns the same keys as before. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
On FreeBSD, NetBSD and DragonFly BSD,
wchar_tvalues are not Unicode code points in non-UTF-8 locales:mbstowcs()andwcstombs()keep the raw locale values (e.g. the KOI8-U byte 0xD0 becomes 0x00D0, and the EUC-JP byte pair 0xB7 0xEE becomes 0xB7EE), and the collation tables and the curses library work with such values. This corrupted every interface which passes locale-encoded text throughwchar_t:time.strftime()(and thereforedatetimeandtime.strptime()) andlocale.nl_langinfo()returned mojibake (e.g.'ÐÏÎÅĦÌÏË'instead of'понеділок'in the uk_UA.KOI8-U locale);locale.localeconv()andformat(n, 'n')returned mojibake monetary and numeric strings;ctypesand other error messages were mojibake;sys.argvand other startup data could not round-trip throughos.fsencode();locale.strcoll()andlocale.strxfrm()produced wrong collation (the collation tables are keyed by the raw locale values, and characters missing from the table are compared by their numeric value);The fix converts between the locale encoding and Unicode with
iconv()in_Py_DecodeLocaleEx()and_Py_EncodeLocaleEx()-- unlike the codec machinery,iconv()is usable in early interpreter startup, and the C library'siconv()supports all encodings used by its own locales.time.strftime()usesstrftime()instead ofwcsftime()(whose output is non-Unicodewchar_t), thelocalemodule usesstrcoll()andstrxfrm()on the locale-encoded strings, and the curses module converts between Unicode and the nativewchar_tform on the library boundary. Ificonv()is not functional for the locale encoding, the current behavior is kept.An alternative -- converting globally in
PyUnicode_FromWideChar()andPyUnicode_AsWideChar()like on Oracle Solaris (HAVE_NON_UNICODE_WCHAR_T_REPRESENTATION) -- was prototyped and rejected:wchar_tat that boundary is also used as a plain Unicode container (e.g.os.path.normpath()is implemented onwchar_t), and on these platforms the raw locale values overlap with Unicode code points, so no boundary policy can distinguish the two uses. It broke 46-126 test files, either with hard errors or with silent corruption (os.path.normpath('café')returned'cafФ'in the KOI8-U locale).Verified on FreeBSD 15.1, NetBSD 10.1 and DragonFly BSD 6.4 with Ukrainian, Greek, German, Turkish, Catalan and Japanese locales in eight encodings: the full test suite has no regressions, previously failing tests (
test_cmd_line.test_invalid_utf8_arg) now pass, andtest_cursespasses in non-UTF-8 locales with the ncursesw backend. The full suite in the uk_UA.KOI8-U locale on FreeBSD now passes with zero failures.Note:
test_logging.test_listen_server_errorin non-UTF-8 locales additionally needs the gh-93251 fix (#154683).