X Tutup
The Wayback Machine - https://web.archive.org/web/20241214185146/https://github.com/python/cpython/issues/44868
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

[doc] mention that setlocale raises exception if given a nonexisting locale #44868

Closed
doko42 opened this issue Apr 19, 2007 · 9 comments
Closed
Assignees
Labels
3.10 only security fixes 3.11 only security fixes 3.12 bugs and security fixes easy extension-modules C modules in the Modules dir type-feature A feature request or enhancement

Comments

@doko42
Copy link
Member

doko42 commented Apr 19, 2007

BPO 1703592
Nosy @malemburg, @loewis, @warsaw, @rhettinger, @doko42, @cjwatson

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 = 'https://github.com/loewis'
closed_at = None
created_at = <Date 2007-04-19.13:35:35.000>
labels = ['easy', '3.9', '3.10', '3.11', 'extension-modules', 'type-feature']
title = '[doc] mention that setlocale raises exception if given a nonexisting locale'
updated_at = <Date 2022-03-21.23:30:55.175>
user = 'https://github.com/doko42'

bugs.python.org fields:

activity = <Date 2022-03-21.23:30:55.175>
actor = 'iritkatriel'
assignee = 'loewis'
closed = False
closed_date = None
closer = None
components = ['Extension Modules']
creation = <Date 2007-04-19.13:35:35.000>
creator = 'doko'
dependencies = []
files = []
hgrepos = []
issue_num = 1703592
keywords = ['easy']
message_count = 8.0
messages = ['55067', '55068', '55069', '55070', '55071', '116662', '116668', '117375']
nosy_count = 7.0
nosy_names = ['lemburg', 'loewis', 'barry', 'rhettinger', 'doko', 'cjwatson', 'docs@python']
pr_nums = []
priority = 'low'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue1703592'
versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

@doko42
Copy link
Member Author

doko42 commented Apr 19, 2007

this came up on #ubuntu-devel; Debian and Ubuntu do not generate all libc locales by default, so it is likely that

$ LC_ALL=en_US.UTF-8 python -c "import locale; locale.setlocale(locale.LC_ALL, '')"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python2.5/locale.py", line 476, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting

fails on a system (often experienced ssh'ing into a server system where your current locale doesn't exist). Examples for bug reports in various applications are:

https://launchpad.net/bugs/91583 (apt-listchanges)
https://launchpad.net/bugs/88638 (pitivi)
https://launchpad.net/bugs/81556 (exaile)
https://launchpad.net/bugs/90525 (hwdb-client)

In C, the result of the setlocale(3) call can be, and usually is ignored, if the locale cannot be set.

It is argued that the Python interface for locale.setlocale() should not raise an exception by default, if the locale is missing on the system.

That would be an behaviour change of locale.setlocale(), so the original behavour should be kept as an option (introducing an optional third parameter to raise the exception when the locale doesn't exist). Is this an appropriate change, or could another change be suggested?

@doko42 doko42 added extension-modules C modules in the Modules dir type-feature A feature request or enhancement labels Apr 19, 2007
@doko42 doko42 added extension-modules C modules in the Modules dir type-feature A feature request or enhancement labels Apr 19, 2007
@loewis
Copy link
Mannequin

loewis mannequin commented Apr 19, 2007

Not raising an exception if an operation failed violates the Zen of Python (errors should never pass silently, unless explicitly silenced). So explicit silencing is necessary, but if so, it's easy enough to explicitly catch the exception:

try:
locale.setlocale(..)
except locale.Error:
pass

If the intention is that all software on Debian works successfully even if the locale is configured incorrectly, then an automated build system should perform all builds in a non-existing locale, and see if anything fails.

@rhettinger
Copy link
Contributor

-1

I prefer explicit exception. If needed, it is not hard to catch and ignore the exception.

@cjwatson
Copy link
Mannequin

cjwatson mannequin commented Apr 20, 2007

If this were actually a true error, I'd agree with you, but it isn't. For most programs (that just do locale.setlocale(locale.LC_ALL, '') to switch on internationalisation), it's a warning, not an error; in the common case there is no reason for them to fail.

If you still insist that this has to be treated as an error, how about adding locale.enable_i18n or something for the common case that does:

try:
locale.setlocale(locale.LC_ALL, '')
except locale.Error:
pass

Because, in practice, many programs appear not to bother catching the exception, and because the programmer is typically using an environment with a properly configured locale they won't notice. It's only when you're in an environment such as sshing (with SendEnv) to a remote system that doesn't have your locale configured that you notice that C programs continue to function correctly, Perl programs issue a warning on stderr, but Python programs crash. While noticing errors is a good thing in general, it seems to go a bit far here.

@loewis
Copy link
Mannequin

loewis mannequin commented Apr 20, 2007

How can you say it's not an error? The function does not achieve what it attempts to.

Adding another function with a different semantics is fine to me, although I doubt it helps: the existing code calls setlocale, not that other function.

@BreamoreBoy
Copy link
Mannequin

BreamoreBoy mannequin commented Sep 17, 2010

Is this still an issue on Debian and Ubuntu?

@cjwatson
Copy link
Mannequin

cjwatson mannequin commented Sep 17, 2010

Yes, the same symptoms are still present.

I'd argue that it generally isn't an error in practice for applications, and thus the net effect of this exception is negative; it's extraordinarily rare for a crash to be preferable to running without localisation. Adding a new function would help because I think it would be easier to persuade people to call a new function that just does what they want ("turn on localisation if possible") than to catch an exception (at which point they have to think "hm, could that exception be for some other reason than you-just-don't-have-that-locale", etc.).

@warsaw
Copy link
Member

warsaw commented Sep 25, 2010

Certainly at a minimum, the docs should describe the exception and workaround instead of just:

"setlocale() is not thread safe on most systems. Applications typically start with a call of

    import locale
    locale.setlocale(locale.LC_ALL, '')

This sets the locale for all categories to the user’s default setting (typically specified in the LANG environment variable). If the locale is not changed thereafter, using multithreading should not cause problems."

+0 for a new function in Python 3.2.

For Python 2.7 though, we can't really add that new function or change the behavior of setlocale() so I'm not sure what the right thing to do is. A documentation change is probably as good as it's going to get for Python 2.

@iritkatriel iritkatriel added easy 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes labels Mar 21, 2022
@iritkatriel iritkatriel changed the title have a way to ignore nonexisting locales in locale.setlocale [doc] mention that setlocale raises exception if given a nonexisting locale Mar 21, 2022
@iritkatriel iritkatriel added 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes labels Mar 21, 2022
@iritkatriel iritkatriel changed the title have a way to ignore nonexisting locales in locale.setlocale [doc] mention that setlocale raises exception if given a nonexisting locale Mar 21, 2022
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
@iritkatriel iritkatriel added 3.12 bugs and security fixes and removed 3.9 only security fixes labels Oct 5, 2022
@iritkatriel
Copy link
Member

This was added to the docs in 395ca72.

@iritkatriel iritkatriel closed this as not planned Won't fix, can't repro, duplicate, stale May 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.10 only security fixes 3.11 only security fixes 3.12 bugs and security fixes easy extension-modules C modules in the Modules dir type-feature A feature request or enhancement
Projects
Status: Done
Development

No branches or pull requests

4 participants
X Tutup