Skip to content

locale.setlocale example incorrect #85204

Closed

Description

@pintman
mannequin
BPO 41032
Nosy @Barathwaja, @pintman

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2020-06-19.09:26:58.820>
labels = ['3.8', 'type-feature', '3.7', 'docs']
title = 'locale.setlocale example incorrect'
updated_at = <Date 2020-08-17.11:31:56.409>
user = 'https://github.com/pintman'

bugs.python.org fields:

activity = <Date 2020-08-17.11:31:56.409>
actor = 'pintman'
assignee = 'docs@python'
closed = False
closed_date = None
closer = None
components = ['Documentation']
creation = <Date 2020-06-19.09:26:58.820>
creator = 'pintman'
dependencies = []
files = []
hgrepos = []
issue_num = 41032
keywords = []
message_count = 3.0
messages = ['371860', '375514', '375541']
nosy_count = 3.0
nosy_names = ['docs@python', 'Barathwaja', 'pintman']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue41032'
versions = ['Python 3.6', 'Python 3.7', 'Python 3.8']

Linked PRs

Activity

  1. pintman commented on Jun 19, 2020

    pintmanmannequin
    MannequinAuthor

    The example given results in an error.

    https://docs.python.org/3.8/library/locale.html?highlight=locale

    >> locale.setlocale(locale.LC_ALL, 'de_DE')

    I could be improved with one of the following versions:

    >> locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8')
    >> locale.setlocale(locale.LC_ALL, ('de_DE'), ('UTF-8'))

  2. added
    docsDocumentation in the Doc dir
    type-featureA feature request or enhancement
    on Jun 19, 2020
  3. added
    docsDocumentation in the Doc dir
    type-featureA feature request or enhancement
    on Jun 19, 2020
  4. Barathwaja commented on Aug 16, 2020

    Barathwajamannequin
    Mannequin

    Hi Marco,

    Can I work on this?

  5. pintman commented on Aug 17, 2020

    pintmanmannequin
    MannequinAuthor

    I have only reported the bug and do not know exactly how to proceed. I don't think there's anything wrong with taking over the bug.

  6. transferred this issue fromon Apr 10, 2022
  7. slateny commented on Apr 22, 2022

    @slateny
    Contributor

    The example seems to fail on Ubuntu, but runs on Windows - there is a comment right above the line that says name might vary with platform. It does seem that locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8') works on both platforms though, so maybe it can be changed to that

  8. 3 remaining items

  9. added
    3.14bugs and security fixes
    and removed on Feb 23, 2025
  10. encukou commented on Feb 24, 2025

    @encukou
    Member

    Depends on what locales are installed; on a minimal Ubuntu container not even 'de_DE.UTF-8' will work.
    IMO it would be good to mention 'de_DE.UTF-8' in the docs, but also note that locale availability (and not just naming) might vary with platform.

  11. picnixz commented on Mar 23, 2025

    @picnixz
    Member

    Adding an other example from #91343 (comment):

    The example in the doc shows

    >>> import locale
    >>> loc = locale.getlocale()  # get current locale
    # use German locale; name might vary with platform
    >>> locale.setlocale(locale.LC_ALL, 'de_DE')
    >>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
    >>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
    >>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
    >>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale
    

    However locale.getlocale() does not return the locale for all categories (locale.LC_ALL is even not allowed) but the locale for the LC_CTYPE category.

    Therefore restoring it using locale.setlocale(locale.LC_ALL, loc) does not actually restore the initial settings, and may even fail on some platforms (on mine it does).

    The correct example should have the first line of code replaced with

    >>> loc = locale.setlocale(locale.LC_ALL)  # get current locale
    

    Note: this issue was first reported in the pandas library at pandas-dev/pandas#46595

  12. added a commit that references this issue on Dec 16, 2025
  13. added 2 commits that reference this issue on Dec 16, 2025
  14. added 2 commits that reference this issue on Dec 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dirtype-featureA feature request or enhancement

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions