Title: Improve documentation of locale encoding functions
Type: Stage: needs patch
Components: Documentation Versions: Python 3.4, Python 3.5
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, eric.araujo, ezio.melotti, gjb1002, lemburg, vstinner
Priority: normal Keywords: patch

Created on 2011-09-30 08:15 by gjb1002, last changed 2019-03-15 23:53 by BreamoreBoy.

File name Uploaded Description Edit
Issue13074.diff BreamoreBoy, 2014-06-25 23:42 Add note recommending getpreferredencoding
Messages (3)
msg144677 - (view) Author: Geoffrey Bache (gjb1002) Date: 2011-09-30 08:15
The locale module provides locale.getdefaultlocale and locale.getpreferredencoding. The encodings returned by each are generally subtly different ('ISO8859-1' vs 'ISO-8859-1'), but the difference between these methods is not explained. 

A comment by Martin von Löwis from 2003 in indicates that "getdefaultlocale should not be used in new code", if this is really the case then this should be in the docs. 

Anyone reading the docs from the top will currently encounter getdefaultlocale first and believe that this is the way to get the encoding.
msg221582 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-06-25 23:42
Hopefully the patch speaks for itself.
msg221596 - (view) Author: Marc-Andre Lemburg (lemburg) * (Python committer) Date: 2014-06-26 07:51
The two functions serve a different purpose.

getdefautltlocale() specifically avoids calling setlocale() and is thread-safe on Unix. It's purpose is to return the default locale string, not only the encoding.

getpreferredencoding() only returns the encoding, but on Unix has to call setlocale() to return correct results and thus is not thread-safe.

Martin's comment doesn't address this difference and I don't agree with it.

Regarding the different results, I guess this could be solved by having both function pass the data obtained from the system through _parse_localname() before returning it, but that would have to be a handled in a new issue report.
Date User Action Args
2019-03-15 23:53:33BreamoreBoysetnosy: - BreamoreBoy
2014-06-26 07:51:33lemburgsetmessages: + msg221596
2014-06-25 23:42:06BreamoreBoysetfiles: + Issue13074.diff
versions: + Python 3.4, Python 3.5, - Python 2.7, Python 3.2, Python 3.3
nosy: + BreamoreBoy

messages: + msg221582

keywords: + patch
2011-10-09 09:04:29eric.araujosetnosy: + lemburg, vstinner, eric.araujo
2011-09-30 08:18:33ezio.melottisetnosy: + ezio.melotti
stage: needs patch

versions: - Python 2.6, Python 3.1, Python 3.4
2011-09-30 08:15:33gjb1002create