locale getlocale returns None on OSX

Albert-Jan Roskam fomcl at yahoo.com
Wed Mar 12 20:40:27 CET 2014



>________________________________
> From: Albert-Jan Roskam <fomcl at yahoo.com>
>To: Python <python-list at python.org> 
>Sent: Wednesday, March 12, 2014 1:22 PM
>Subject: locale getlocale returns None on OSX
> 
>
>Hi,
>
>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
>
>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.
>
>Regards,
>
>Albert-Jan


Ok, here are some tests on my own system:

albertjan at debian:~$ uname -a
Linux debian 3.2.0-4-amd64 #1 SMP Debian 3.2.54-2 x86_64 GNU/Linux


Even if locale.setlocale is used first, OSX returns (None, None) when locale.getlocale() is called. Another thing that surprises me in the examples below is the output of the "python -c" example using Python 2.7. Isn't this supposed to be exactly equivalent to the code that follows?



# ======== Python 2.7 ========albertjan at debian:~$ python -c "import locale; locale.setlocale(locale.LC_ALL, ""); print(locale.getlocale())"

(None, None)   # <---- why is this?


albertjan at debian:~$ python
Python 2.7.3 (default, Jan  2 2013, 13:56:14) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
>>> locale.setlocale(locale.LC_ALL, "")
'en_US.UTF-8'
>>> locale.getlocale()
('en_US', 'UTF-8')  # <--- OSX (sometimes?) returns (None, None) here.



# ======== Python 3.3 ========
albertjan at debian:~$ python3 -c "import locale; locale.setlocale(locale.LC_ALL, ""); print(locale.getlocale())"
('en_US', 'UTF-8')

albertjan at debian:~$ python3
Python 3.3.4 (default, Feb 17 2014, 19:23:00) 
[GCC 4.7.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
>>> locale.setlocale(locale.LC_ALL, "")
'en_US.UTF-8'
>>> locale.getlocale()
('en_US', 'UTF-8')



# ======== Pypy ========
albertjan at debian:~$ pypy -c "import locale; locale.setlocale(locale.LC_ALL, ""); print locale.getlocale()"
(None, None)

albertjan at debian:~$ pypy

Python 2.7.3 (87aa9de10f9ca71da9ab4a3d53e0ba176b67d086, Mar 10 2014, 14:07:15)
[PyPy 2.2.1 with GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
And now for something completely different: ``pypy is a better kind of
foolishness - lac''
>>>> import locale
>>>> locale.setlocale(locale.LC_ALL, "")
'en_US.UTF-8'
>>>> locale.getlocale()
('en_US', 'UTF-8')



More information about the Python-list mailing list