locale.getlocale() returns wrong encoding if the locale name doesn't have one
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 36k
- 平均合併
- 1 天 9 小時
- 30 天內合併 PR
- 558
描述
Bug report
Bug description:
Some locales don't have an encoding especifier, and python (wrongly) assumes it is ISO-8859-1, when it may not be. For example, the following code:
import locale
l = locale.getlocale()
print(f"locale: {l}, encoding: {locale.getencoding()}")
locale.setlocale(locale.LC_ALL, l)
Returns:
$ LANG=en_GB.UTF-8 python3 test.py
locale: ('en_GB', 'UTF-8'), encoding: UTF-8
$ LANG=en_IN python3 test.py
locale: ('en_IN', 'ISO8859-1'), encoding: UTF-8
Traceback (most recent call last):
File "/home/emilio/profesional/freexian-ext/test.py", line 5, in <module>
locale.setlocale(locale.LC_ALL, l)
~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.13/locale.py", line 615, in setlocale
return _setlocale(category, locale)
locale.Error: unsupported locale setting
Traceback (most recent call last):
File "/home/emilio/test.py", line 5, in <module>
locale.setlocale(locale.LC_ALL, l)
~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.13/locale.py", line 615, in setlocale
return _setlocale(category, locale)
locale.Error: unsupported locale setting
The en_IN locale uses UTF-8, which is correctly returned in locale.getencoding(), but not in locale.getlocale(). Using the tuple returned by locale.getlocale() in a setlocale call fails.
This probably affects other locales that use UTF-8 as their encoding but don't especify it in the locale name.
This was tested on Debian stable (trixie) with Python 3.13.
CPython versions tested on:
3.13
Operating systems tested on:
Linux
Linked PRs
- gh-155549
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
報告指向 locale.py,並包含一個可重現的 test.py 範例;先在 Linux 上使用 LANG=en_IN 執行它,並比較 locale.getlocale() 與 locale.getencoding()。當對於名稱中省略編碼的 locale,locale.getlocale() 能保留可用的 UTF-8 編碼,使其結果傳遞給 locale.setlocale() 時不再失敗,就表示完成。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- localization
- Issue 類型
- 缺陷
- 難度
- 3/5
- 預估耗時
- 1-2 天
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100