GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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

@serhiy-storchaka serhiy-storchaka commented Jul 26, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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).

  • Issue: Locale-encoded text is corrupted on FreeBSD, NetBSD, DragonFly BSD and macOS in non-UTF-8 locales #154682

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@serhiy-storchaka
serhiy-storchaka requested review from StanFromIreland and pganssle as code owners July 26, 2026 09:30
@bedevere-app bedevere-app Bot mentioned this pull request Jul 26, 2026
Locale-encoded text is corrupted on FreeBSD, NetBSD, DragonFly BSD and macOS in non-UTF-8 locales #154682
Open
@bedevere-app bedevere-app Bot added the awaiting core review label Jul 26, 2026
@serhiy-storchaka @claude
pythongh-154682: Fix corruption of locale-encoded text on FreeBSD, Ne…
56ee379
…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
serhiy-storchaka force-pushed the gh-154682-bsd-locale-iconv branch from ef44c79 to 56ee379 Compare July 26, 2026 11:28
@serhiy-storchaka 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
@serhiy-storchaka
serhiy-storchaka marked this pull request as draft July 26, 2026 12:26
@bedevere-app bedevere-app Bot removed the awaiting core review label Jul 26, 2026
serhiy-storchaka and others added 3 commits July 26, 2026 15:44
@serhiy-storchaka @claude
Do not fail if iconv() cannot decode what mbstowcs() can
44f96df
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]>
@serhiy-storchaka @claude
Fix the decimal point and the thousands separator in the decimal module
94ee8e2
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]>
@serhiy-storchaka @claude
Avoid the overhead of the conversion in UTF-8 and ASCII locales
423e68c
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]>
@github-actions

github-actions Bot commented Oct 29, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 90 days with no activity.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@pganssle pganssle Awaiting requested review from pganssle pganssle is a code owner
@StanFromIreland 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.

Projects

None yet

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.

1 participant

@serhiy-storchaka
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.