GeezerVison is showing github.com — gh-154682: Fix corruption of locale-encoded text on FreeBSD, NetBSD, DragonFly BSD and macOS by serhiy-storchaka · Pull Request #154722 · python/cpython
Conversation
serhiy-storchaka
commented
Jul 26, 2026
•
edited by bedevere-app
Bot
Loading
Uh oh!
There was an error while loading. Please reload this page.
On FreeBSD, NetBSD and DragonFly BSD, wchar_t values are not Unicode code points in non-UTF-8 locales: mbstowcs() and wcstombs() 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 through wchar_t:
time.strftime() (and therefore datetime and time.strptime()) and locale.nl_langinfo() returned mojibake (e.g. 'ÐÏÎÅĦÌÏË' instead of 'понеділок' in the uk_UA.KOI8-U locale);
locale.localeconv() and format(n, 'n') returned mojibake monetary and numeric strings;
- localized OSError,
ctypes and other error messages were mojibake;
sys.argv and other startup data could not round-trip through os.fsencode();
locale.strcoll() and locale.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 wide-character curses API could not display or read non-ASCII characters at all (Cyrillic text in a Cyrillic locale was rendered as blanks).
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's iconv() supports all encodings used by its own locales. time.strftime() uses strftime() instead of wcsftime() (whose output is non-Unicode wchar_t), the locale module uses strcoll() and strxfrm() on the locale-encoded strings, and the curses module converts between Unicode and the native wchar_t form on the library boundary. If iconv() is not functional for the locale encoding, the current behavior is kept.
An alternative -- converting globally in PyUnicode_FromWideChar() and PyUnicode_AsWideChar() like on Oracle Solaris (HAVE_NON_UNICODE_WCHAR_T_REPRESENTATION) -- was prototyped and rejected: wchar_t at that boundary is also used as a plain Unicode container (e.g. os.path.normpath() is implemented on wchar_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, and test_curses passes 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_error in non-UTF-8 locales additionally needs the gh-93251 fix (#154683).
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
…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 <[email protected]>
serhiy-storchaka
changed the title
gh-154682: Fix corruption of locale-encoded text on FreeBSD, NetBSD and DragonFly BSD
gh-154682: Fix corruption of locale-encoded text on FreeBSD, NetBSD, DragonFly BSD and macOS
Jul 26, 2026
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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
|
This PR is stale because it has been open for 90 days with no activity.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
pganssle
Awaiting requested review from pganssle
pganssle is a code owner
StanFromIreland
Awaiting requested review from StanFromIreland
StanFromIreland is a code owner
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Labels
stale
Stale PR or inactive for long period of time.
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
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.
|