python / python/cpython

locale.getlocale() returns wrong encoding if the locale name doesn't have one

Open
#151,316 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib topic-unicode type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The report points to locale.py and includes a reproducible test.py example; start by running it on Linux with LANG=en_IN and comparing locale.getlocale() with locale.getencoding(). Done means locale.getlocale() preserves the usable UTF-8 encoding for locales whose names omit an encoding, so passing its result to locale.setlocale() no longer fails.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
localization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.