Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

locale module example is wrong for some platforms #91343

Open
smarie mannequin opened this issue Apr 1, 2022 · 1 comment
Open

locale module example is wrong for some platforms #91343

smarie mannequin opened this issue Apr 1, 2022 · 1 comment
Labels
3.10 only security fixes docs Documentation in the Doc dir

Comments

@smarie
Copy link
Mannequin

smarie mannequin commented Apr 1, 2022

BPO 47187
Nosy @smarie

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 2022-04-01.13:45:07.191>
labels = ['3.10', 'docs']
title = 'locale module example is wrong for some platforms'
updated_at = <Date 2022-04-01.13:45:07.191>
user = 'https://github.com/smarie'

bugs.python.org fields:

activity = <Date 2022-04-01.13:45:07.191>
actor = 'smarie'
assignee = 'docs@python'
closed = False
closed_date = None
closer = None
components = ['Documentation']
creation = <Date 2022-04-01.13:45:07.191>
creator = 'smarie'
dependencies = []
files = []
hgrepos = []
issue_num = 47187
keywords = []
message_count = 1.0
messages = ['416487']
nosy_count = 2.0
nosy_names = ['docs@python', 'smarie']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = None
url = 'https://bugs.python.org/issue47187'
versions = ['Python 3.10']

@smarie
Copy link
Mannequin Author

smarie mannequin commented Apr 1, 2022

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

@smarie smarie mannequin added the 3.10 only security fixes label Apr 1, 2022
@smarie smarie mannequin assigned docspython Apr 1, 2022
@smarie smarie mannequin added docs Documentation in the Doc dir 3.10 only security fixes labels Apr 1, 2022
@smarie smarie mannequin assigned docspython Apr 1, 2022
@smarie smarie mannequin added the docs Documentation in the Doc dir label Apr 1, 2022
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.10 only security fixes docs Documentation in the Doc dir
Projects
Status: No status
Development

No branches or pull requests

0 participants