Skip to content

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

@epozuelo

Description

@epozuelo

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error
    No fields configured for issues without a type.

    Projects

    Status
    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions