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


Groups > comp.lang.python > #80489

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

From Terry Reedy <tjreedy@udel.edu>
Subject Re: locale.getlocale() in cmd.exe vs. Idle
Date 2014-11-10 15:31 -0500
References <37011265.138748.1415611323250.JavaMail.yahoo@jws10709.mail.gq1.yahoo.com>
Newsgroups comp.lang.python
Message-ID <mailman.15639.1415651514.18130.python-list@python.org> (permalink)

Show all headers | View raw


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

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


Thread

Re: locale.getlocale() in cmd.exe vs. Idle Terry Reedy <tjreedy@udel.edu> - 2014-11-10 15:31 -0500

csiph-web