locale.getlocale() returns wrong encoding if the locale name doesn't have one
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 77.2k
- Fork
- 35.9k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Il report rimanda a locale.py e include un esempio riproducibile in test.py; inizia eseguendolo su Linux con LANG=en_IN e confrontando locale.getlocale() con locale.getencoding(). Il lavoro è completato quando locale.getlocale() conserva la codifica UTF-8 utilizzabile per le locale i cui nomi omettono una codifica, in modo che passare il suo risultato a locale.setlocale() non causi più errori.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- localization
- Tipo di issue
- Bug
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100