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


Groups > comp.lang.python > #80489 > unrolled thread

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

Started byTerry Reedy <tjreedy@udel.edu>
First post2014-11-10 15:31 -0500
Last post2014-11-10 15:31 -0500
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

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

FromTerry Reedy <tjreedy@udel.edu>
Date2014-11-10 15:31 -0500
SubjectRe: locale.getlocale() in cmd.exe vs. Idle
Message-ID<mailman.15639.1415651514.18130.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web