locale.getlocale() in cmd.exe vs. Idle

Terry Reedy tjreedy at udel.edu
Mon Nov 10 15:31:34 EST 2014


On 11/10/2014 4:22 AM, Albert-Jan Roskam wrote:
> Hi,
>
> Why do I get different output for locale.getlocale() in Idle vs. cmd.exe?
>
> # IDLE
> Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32
> Type "copyright", "credits" or "license()" for more information.
>>>> import locale
>>>> locale.getdefaultlocale()
> ('nl_NL', 'cp1252')
>>>> locale.getlocale()
> ('Dutch_Netherlands', '1252')  # I need this specific notation
>>>>
>
> # cmd.exe or Ipython
> C:\Users\albertjan>python
> Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import locale
>>>> locale.getdefaultlocale()
> ('nl_NL', 'cp1252')
>>>> locale.getlocale()
> (None, None)
>
> # using setlocale does work (one of these instances when I answer my own question while writing to the Python list)
> C:\Users\albertjan>python
> Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import locale
>>>> locale.setlocale(locale.LC_ALL, "")
> 'Dutch_Netherlands.1252'
>>>> locale.getlocale()
> ('Dutch_Netherlands', '1252')

Idle runs code in an environment that is slightly altered from the 
standard python startup environment'.  idlelib.IOBinding has this
'''
# Try setting the locale, so that we can find out
# what encoding to use
try:
     import locale
     locale.setlocale(locale.LC_CTYPE, "")
'''
idlelib.run, which runs in the user-code subprocess, imports IOBinding. 
Setting LC_CTYPE is sufficient for getlocale() to not return null values.

C:\Users\Terry>python -c "import locale; print(locale.getlocale())"
(None, None)

C:\Users\Terry>python -c "import locale; 
locale.setlocale(locale.LC_CTYPE, ''); print(locale.getlocale())"
('English_United States', '1252')

-- 
Terry Jan Reedy




More information about the Python-list mailing list