Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #68297

Re: locale getlocale returns None on OSX

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'encoding': 0.05; 'explicitly': 0.05; '(python': 0.07; 'environments': 0.07; 'explicit': 0.07; 'session.': 0.07; 'variables': 0.07; 'locale': 0.09; 'none)': 0.09; 'outlined': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:None': 0.09; 'used.': 0.09; 'python': 0.11; '(none,': 0.16; 'caveats': 0.16; 'it;': 0.16; 'lang': 0.16; 'name)': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'variables,': 0.16; 'weird': 0.16; 'wrote:': 0.18; 'do.': 0.18; 'variable': 0.18; 'app': 0.19; 'module': 0.19; "python's": 0.19; 'settings': 0.22; 'preferred': 0.22; 'shell': 0.22; 'this?': 0.23; 'header:User-Agent:1': 0.23; 'module,': 0.24; "shouldn't": 0.24; 'environment': 0.24; 'header:X -Complaints-To:1': 0.27; 'character': 0.29; 'points': 0.29; 'specified': 0.30; 'program,': 0.31; 'ok.': 0.31; 'though.': 0.31; 'probably': 0.32; 'run': 0.32; 'url:python': 0.33; 'running': 0.33; 'could': 0.34; 'problem': 0.35; 'something': 0.35; 'but': 0.35; 'set.': 0.36; 'charset:us-ascii': 0.36; 'possible': 0.36; 'url:org': 0.36; 'changing': 0.37; 'so,': 0.37; 'starting': 0.37; 'being': 0.38; 'sometimes': 0.38; 'url:library': 0.38; 'needed': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'received:org': 0.40; 'read': 0.60; 'break': 0.61; 'profile': 0.61; 'first': 0.61; 'here:': 0.62; 'provide': 0.64; 'side': 0.67; 'default': 0.69; 'user,': 0.69; 'programs,': 0.74; 'received:204': 0.75; 'article': 0.77; 'env': 0.84; 'received:204.14': 0.84; 'thoroughly': 0.91; 'vars': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ned Deily <nad@acm.org>
Subject Re: locale getlocale returns None on OSX
Date Wed, 12 Mar 2014 13:43:44 -0700
References <1394626979.46880.YahooMailBasic@web163806.mail.gq1.yahoo.com>
Mime-Version 1.0
Content-Type text/plain; charset=US-ASCII
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host 204.14.154.233
User-Agent MT-NewsWatcher/3.5.3b3 (Intel Mac OS X)
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.8103.1394657037.18130.python-list@python.org> (permalink)
Lines 48
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1394657037 news.xs4all.nl 2845 [2001:888:2000:d::a6]:56831
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:68297

Show key headers only | View raw


In article 
<1394626979.46880.YahooMailBasic@web163806.mail.gq1.yahoo.com>,
 Albert-Jan Roskam <fomcl@yahoo.com> wrote:
> locale.getlocale() sometimes returns (None, None) under OSX (Python 2, not 
> sure about Python 3, but I think so). The problem is outlined here:
> http://stackoverflow.com/questions/1629699/locale-getlocale-problems-on-osx

Python's locale uses the plaform's locale implementation and POSIX 
locale is an old and somewhat weird animal.  Make sure you thoroughly 
read the description of the locale module, in particular, the caveats 
section:

http://docs.python.org/2/library/locale.html#background-details-hints-tip
s-and-caveats

The first gotcha is that you need to explicitly call 
locale.setlocale("LC_ALL,"") to get the preferred locale environment, as 
either specified by the user, by LANG or other LC_* environment 
variables, or the platform defaults.  In general, OS X does not provide 
a default for your process.  However, various environments do.  The OS X 
Terminal.app has profile settings (Preferences -> Settings -> (Profile 
name) -> Advanced) to specific a character encoding and a checkbox to 
"Set locale environment variables on startup".  With that set properly, 
programs run under Terminal.app will see LANG set.  The user can also 
set an explicit LANG value in a shell profile, like .profile or .bashrc, 
but those only apply when a shell is being used.  Depending on which 
profile it is set in, that might not work under all conditions, like 
under "sudo" or in an "ssh" session.  Setting an env variable in a shell 
profile would also not apply to an double-clickable app bundle since no 
shell is involved in launching it; it is possible to set environment 
variables in the app's Info.plist, though.
 
> What is the cause of this? Is it limited to just Darwin systes? Does the 
> 'horrible hack' solution on OS have any drawbacks? I like it better because 
> it is not needed to set the LC_ALL environment variable prior to starting the 
> Python program.

As the caveats section points out, setting locale env vars may have 
unwanted side effects on other parts of the process it is running in or 
creates.  So, if you are using it in a standalone program, it may be OK.  
If you are using it a module intended to be used by other programs, you 
probably shouldn't be changing something that could break other parts of 
the calling program.

-- 
 Ned Deily,
 nad@acm.org

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: locale getlocale returns None on OSX Ned Deily <nad@acm.org> - 2014-03-12 13:43 -0700

csiph-web