Skip to content

Accept C and POSIX in Locale.parse - #1340

Merged
akx merged 2 commits into
python-babel:masterfrom
gyanu2507:fix/parse-c-posix-locale
Sep 21, 2026
Merged

akx merged 2 commits into
python-babel:masterfrom
gyanu2507:fix/parse-c-posix-locale

Conversation

@gyanu2507

@gyanu2507 gyanu2507 commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1236.

default_locale() maps C, POSIX, and C.UTF-8 to en_US_POSIX. Locale.parse() did not, so Locale.parse("C") raised UnknownLocaleError: unknown locale 'c'. That is the frame in the report.

parse() now applies the same mapping (case-insensitive, optional encoding suffix) before parse_locale. en_US_POSIX with a POSIX variant is unchanged.

default_locale already maps those to en_US_POSIX. parse() did not, so a
C locale from the environment raised UnknownLocaleError.
Comment thread babel/core.py Outdated

# C/POSIX is not a CLDR language. Same mapping default_locale uses.
posix_stem = identifier.split(".")[0].split("@")[0]
posix_stem = posix_stem.replace("-", "_")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this replace necessary? "C" and "POSIX" don't include dashes or underscores.

Comment thread babel/core.py Outdated
raise TypeError(f"Unexpected value for identifier: {identifier!r}")

# C/POSIX is not a CLDR language. Same mapping default_locale uses.
posix_stem = identifier.split(".")[0].split("@")[0]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the @ splitting for? The test case doesn't seem to exercise it, and in any case a @ modifier would be in the postfix already split out by .split(".").

Additionally, please use .partition(".") in cases like this for performance.

C and POSIX never include a dash or @ modifier on the language stem, so the extra replace and split were noise.
@gyanu2507

Copy link
Copy Markdown
Contributor Author

Dropped the dash replace and the @ split. C / POSIX never have those on the language stem, and the encoding is already off the first .. Switched that check to partition(".").

@codecov

codecov Bot commented Sep 21, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.37%. Comparing base (6ba6701) to head (d28b849).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1340   +/-   ##
=======================================
  Coverage   93.37%   93.37%           
=======================================
  Files          27       27           
  Lines        4919     4922    +3     
=======================================
+ Hits         4593     4596    +3     
  Misses        326      326           
Flag Coverage Δ
macos-14-3.10 92.46% <100.00%> (+<0.01%) ⬆️
macos-14-3.11 92.40% <100.00%> (+<0.01%) ⬆️
macos-14-3.12 92.60% <100.00%> (+<0.01%) ⬆️
macos-14-3.13 92.60% <100.00%> (+<0.01%) ⬆️
macos-14-3.14 92.58% <100.00%> (+<0.01%) ⬆️
macos-14-3.15 92.58% <100.00%> (+<0.01%) ⬆️
macos-14-3.8 92.33% <100.00%> (+<0.01%) ⬆️
macos-14-3.9 92.39% <100.00%> (+<0.01%) ⬆️
macos-14-pypy3.10 92.46% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.10 92.48% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.11 92.42% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.12 92.62% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.13 92.62% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.14 92.60% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.15 92.60% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.8 92.35% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.9 92.41% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-pypy3.10 92.48% <100.00%> (+<0.01%) ⬆️
windows-2022-3.10 92.47% <100.00%> (+<0.01%) ⬆️
windows-2022-3.11 92.41% <100.00%> (+<0.01%) ⬆️
windows-2022-3.12 92.61% <100.00%> (+<0.01%) ⬆️
windows-2022-3.13 92.61% <100.00%> (+<0.01%) ⬆️
windows-2022-3.14 92.59% <100.00%> (+<0.01%) ⬆️
windows-2022-3.15 92.59% <100.00%> (+<0.01%) ⬆️
windows-2022-3.8 92.45% <100.00%> (+<0.01%) ⬆️
windows-2022-3.9 92.40% <100.00%> (+<0.01%) ⬆️
windows-2022-pypy3.10 92.47% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@codspeed

codspeed Bot commented Sep 21, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 88 untouched benchmarks


Comparing gyanu2507:fix/parse-c-posix-locale (d28b849) with master (f3aacdc)

Open in CodSpeed

@akx akx left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@akx
akx merged commit c07d94b into python-babel:master Sep 21, 2026
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Babel fails to handle C locale properly

2 participants