classification
Title: calendar does not restore locale properly
Type: Stage: test needed
Components: Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: JJeffries, Retro, christian.heimes, eric.araujo, georg.brandl, ixokai, r.david.murray, tim.golden, twouters
Priority: normal Keywords:

Created on 2010-10-13 19:53 by belopolsky, last changed 2010-12-01 21:44 by r.david.murray. This issue is now closed.

Files
File name Uploaded Description Edit
unnamed Retro, 2010-12-01 21:21
Messages (19)
msg118570 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2010-10-13 19:53
>>> import calendar
>>> calendar.TextCalendar().formatmonthname(2010,10,10)
'October 2010'
>>> calendar.LocaleTextCalendar(locale='fr_FR').formatmonthname(2010,10,10)
'octobre 2010'
>>> calendar.TextCalendar().formatmonthname(2010,10,10)
'octobre 2010'
msg119153 - (view) Author: Boštjan Mejak (Retro) Date: 2010-10-19 18:29
It seems that calendar.LocaleTextCalendar() changes the locale for the current interpreter session to the one you pass to argument 'locale'. This is a bug to be fixed.
msg119155 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-10-19 18:36
Indeed.  Would you like to try to make a patch for Python 3.2?  Help is here: http://www.python.org/dev/ (especially http://www.python.org/dev/patches/ ) and here: http://docs.pythonsprints.com/core_development/beginners.html
msg119157 - (view) Author: Boštjan Mejak (Retro) Date: 2010-10-19 18:50
>
> Would you like to try to make a patch for Python 3.2?

I'm not that geeky. :)

On Tue, Oct 19, 2010 at 8:36 PM, Éric Araujo <report@bugs.python.org> wrote:

>
> Éric Araujo <merwok@netwok.org> added the comment:
>
> Indeed.  Would you like to try to make a patch for Python 3.2?  Help is
> here: http://www.python.org/dev/ (especially
> http://www.python.org/dev/patches/ ) and here:
> http://docs.pythonsprints.com/core_development/beginners.html
>
> ----------
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue10092>
> _______________________________________
>
msg119158 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2010-10-19 18:55
Fixed in r85728.  The problem was that unlike other system calls, setlocale() doesn't return the old setting but the new setting.  The context manager that resets the locale therefore didn't work as intended.
msg119189 - (view) Author: Stephen Hansen (ixokai) (Python triager) Date: 2010-10-20 06:15
I can't be entirely sure, because a) I have never even glanced at the calendar module, and b) my locale-fu is very weak, but my buildbot has consistently failed on this test since this commit:


======================================================================
ERROR: test_localecalendars (test.test_calendar.CalendarTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/pythonbuildbot/buildarea/3.x.hansen-osx-x86/build/Lib/test/test_calendar.py", line 264, in test_localecalendars
    locale=def_locale).formatmonthname(2010, 10, 10)
  File "/Users/pythonbuildbot/buildarea/3.x.hansen-osx-x86/build/Lib/calendar.py", line 520, in formatmonthname
    with different_locale(self.locale):
  File "/Users/pythonbuildbot/buildarea/3.x.hansen-osx-x86/build/Lib/calendar.py", line 490, in __enter__
    _locale.setlocale(_locale.LC_TIME, self.locale)
  File "/Users/pythonbuildbot/buildarea/3.x.hansen-osx-x86/build/Lib/locale.py", line 538, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting

I will look into it in more detail tomorrow to try to provide more meaningful feedback, but I think this "fix" has introduced a problem. If someone sees what before I have time to dig into this unfamiliar territory, yay. :)
msg119191 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2010-10-20 06:53
Let's see if r85735 fixed this.
msg119194 - (view) Author: Boštjan Mejak (Retro) Date: 2010-10-20 08:52
>>> import calendar
>>> calendar.LocaleTextCalendar(locale='fr_FR').formatmonthname(2010,10,10)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\calendar.py", line 522, in formatmonthname
    with TimeEncoding(self.locale) as encoding:
  File "C:\Python27\lib\calendar.py", line 489, in __enter__
    self.oldlocale = _locale.setlocale(_locale.LC_TIME, self.locale)
  File "C:\Python27\lib\locale.py", line 531, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting

Is it just my machine or is this a common thing on most machines? If I do a test and I try to set a locale with the setlocale() method, I get this "locale.Error: unsupported locale setting". So I gather that the method setlocale() is broken and we should fix it before fixing the Locale{Text,HTML}Calendar ones.


The way setlocale() is implemented, is this:

def setlocale(category, value=None):
        """ setlocale(integer,string=None) -> string.
            Activates/queries locale processing.
        """
        if value not in (None, '', 'C'):
            raise Error, '_locale emulation only supports "C" locale'
        return 'C'


Please note that the online documentation documents its API like this:
locale.setlocale(category[, locale])  See http://docs.python.org/library/locale.html#locale.setlocale

So you can see that the implementation differs from the documentation in that it documents the 2nd argument as 'locale' where in the implementation there is 'value'. If that's a bug, I don't know.

Anyway, please fix the implementation. I found out some very interesting thing... Note the line  "if value not in (None, '', 'C')"  in the implementation. This is the faulty thing in the function because NoneType is not iterable.

Test it for yourself in the interpreter:
>>> value = None
>>> value not in None
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: argument of type 'NoneType' is not iterable

So this setlocale() always raises an error. Please fix this.

Of course, this is valid:
>>> value = None
>>> value is not None
False

No error here. So try to combine this into a workable patch. Thanks.
msg119195 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2010-10-20 09:18
Bostjan, both your points are invalid.  First, the locale settings that a machine supports vary greatly.  "fr_FR" doesn't need to be a valid setting on your machine.

Second, "val in None" will always fail.  "val in (None, ...)" will succeed, since you're testing membership in the given tuple that includes None.
msg119197 - (view) Author: Tim Golden (tim.golden) * (Python committer) Date: 2010-10-20 09:37
Boštjan, the code segment you quote is the *fallback* if the
C module hasn't been built for some reason. The module simply
calls through to the underlying C Library. I notice you're
running on Windows, so this is a useful MS page:

http://msdn.microsoft.com/en-us/library/x99tb11d%28VS.71%29.aspx

and you can see there that a call of setlocale (LC_ALL, "English")
is valid (of "French" if you prefer):

Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 32 bit 
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
>>> locale.setlocale (locale.LC_ALL, "French")
'French_France.1252'
>>>
msg119225 - (view) Author: Boštjan Mejak (Retro) Date: 2010-10-20 17:04
Thank you so much for your answer. The
locale.setlocale(category=locale.LC_NUMERIC,
locale="Slovenian")  works like a charm in my application. Now the 'n'
format specifier works as I want. But tell me whether the 'n' format
specifier can be forced to round the float to just one decimal place. I know
that the 'f' format specifier does that by specifying ".1f", but 'f' is not
locale-aware. I have set the 'n' format specifier in my application like
".3n", which is okay if the returned number is two integers and one decimal,
but is not okay if the returned number is one integer and two decimals,
because I want just one decimal, always. How can I make that by using the
'n' format specifier?

On Wed, Oct 20, 2010 at 11:37 AM, Tim Golden <report@bugs.python.org> wrote:

>
> Tim Golden <mail@timgolden.me.uk> added the comment:
>
> Boštjan, the code segment you quote is the *fallback* if the
> C module hasn't been built for some reason. The module simply
> calls through to the underlying C Library. I notice you're
> running on Windows, so this is a useful MS page:
>
> http://msdn.microsoft.com/en-us/library/x99tb11d%28VS.71%29.aspx
>
> and you can see there that a call of setlocale (LC_ALL, "English")
> is valid (of "French" if you prefer):
>
> Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 32 bit
> (Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import locale
> >>> locale.setlocale (locale.LC_ALL, "French")
> 'French_France.1252'
> >>>
>
> ----------
> nosy: +tim.golden
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue10092>
> _______________________________________
>
msg119257 - (view) Author: Boštjan Mejak (Retro) Date: 2010-10-21 01:43
Please respond...

On Wed, Oct 20, 2010 at 7:05 PM, Boštjan Mejak <report@bugs.python.org>wrote:

>
> Boštjan Mejak <bostjan.mejak@gmail.com> added the comment:
>
> Thank you so much for your answer. The
> locale.setlocale(category=locale.LC_NUMERIC,
> locale="Slovenian")  works like a charm in my application. Now the 'n'
> format specifier works as I want. But tell me whether the 'n' format
> specifier can be forced to round the float to just one decimal place. I
> know
> that the 'f' format specifier does that by specifying ".1f", but 'f' is not
> locale-aware. I have set the 'n' format specifier in my application like
> ".3n", which is okay if the returned number is two integers and one
> decimal,
> but is not okay if the returned number is one integer and two decimals,
> because I want just one decimal, always. How can I make that by using the
> 'n' format specifier?
>
> On Wed, Oct 20, 2010 at 11:37 AM, Tim Golden <report@bugs.python.org>
> wrote:
>
> >
> > Tim Golden <mail@timgolden.me.uk> added the comment:
> >
> > Boštjan, the code segment you quote is the *fallback* if the
> > C module hasn't been built for some reason. The module simply
> > calls through to the underlying C Library. I notice you're
> > running on Windows, so this is a useful MS page:
> >
> > http://msdn.microsoft.com/en-us/library/x99tb11d%28VS.71%29.aspx
> >
> > and you can see there that a call of setlocale (LC_ALL, "English")
> > is valid (of "French" if you prefer):
> >
> > Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 32 bit
> > (Intel)] on win32
> > Type "help", "copyright", "credits" or "license" for more information.
> > >>> import locale
> > >>> locale.setlocale (locale.LC_ALL, "French")
> > 'French_France.1252'
> > >>>
> >
> > ----------
> > nosy: +tim.golden
> >
> > _______________________________________
> > Python tracker <report@bugs.python.org>
> > <http://bugs.python.org/issue10092>
> > _______________________________________
> >
>
> ----------
> Added file: http://bugs.python.org/file19306/unnamed
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue10092>
> _______________________________________
>
msg119259 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-10-21 02:43
The bug tracker is not an appropriate place to get help on using Python.  Please ask your question on a forum where you are more likely to get help, such as python-list.
msg119268 - (view) Author: Tim Golden (tim.golden) * (Python committer) Date: 2010-10-21 07:46
I'm afraid this falls outside my particular area of knowledge, and also 
outside the remit of the bug tracker. Perhaps you could post to 
python-list to see if anyone there can help?
msg123013 - (view) Author: Boštjan Mejak (Retro) Date: 2010-12-01 21:06
>>> calendar.LocaleTextCalendar(locale='fr_FR').formatmonthname(2010,10,10)
is not valid because 'fr_FR' is not a valid value for the 'locale' argument

What is valid is this:
>>> calendar.LocaleTextCalendar(locale='French').formatmonthname(2010,10,10)
msg123014 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2010-12-01 21:16
Boštjan,

Please don't add comments to closed issues.  If you believe there is a remaining issue, please file a new report.  Issue numbers are in good supply!

This issue was fixed in r85728.  It is best when this information is the last message on a closed issue.
msg123015 - (view) Author: Boštjan Mejak (Retro) Date: 2010-12-01 21:21
Yes, I know this issue is closed but I wonder how could your Python
interpreter not error on
>>> calendar.LocaleTextCalendar(locale='fr_FR').formatmonthname(2010,10,10)

Please retry executing the above line of code in Python 2.7.1 interpreter
and tell me if you get an error. I get this:
>>> import calendar
>>> calendar.LocaleTextCalendar(locale='fr_FR').formatmonthname(2010,10,10)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\calendar.py", line 522, in formatmonthname
    with TimeEncoding(self.locale) as encoding:
  File "C:\Python27\lib\calendar.py", line 490, in __enter__
    _locale.setlocale(_locale.LC_TIME, self.locale)
  File "C:\Python27\lib\locale.py", line 531, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting
msg123018 - (view) Author: Stephen Hansen (ixokai) (Python triager) Date: 2010-12-01 21:39
On windows, "France" may work and "fr_FR" may not; yet on OSX its exactly the opposite. Its not consistant across platforms.
msg123019 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-12-01 21:44
Boštjan, please see issue 10466 for further information about your question on fr_FR vs French.  Windows, as usual, does not follow the standards.
History
Date User Action Args
2010-12-01 21:44:03r.david.murraysetmessages: + msg123019
2010-12-01 21:39:50ixokaisetnosy: twouters, georg.brandl, ixokai, christian.heimes, tim.golden, eric.araujo, Retro, r.david.murray, JJeffries
messages: + msg123018
2010-12-01 21:27:50belopolskysetnosy: - belopolsky
2010-12-01 21:21:32Retrosetfiles: + unnamed

messages: + msg123015
2010-12-01 21:16:10belopolskysetmessages: + msg123014
2010-12-01 21:06:49Retrosetmessages: + msg123013
2010-12-01 21:02:41Retrosetfiles: - unnamed
2010-12-01 21:02:36Retrosetfiles: - unnamed
2010-10-21 07:46:52tim.goldensetmessages: + msg119268
2010-10-21 02:43:49r.david.murraysetnosy: + r.david.murray
messages: + msg119259
2010-10-21 01:43:27Retrosetfiles: + unnamed

messages: + msg119257
2010-10-20 17:04:58Retrosetfiles: + unnamed

messages: + msg119225
2010-10-20 09:37:57tim.goldensetnosy: + tim.golden
messages: + msg119197
2010-10-20 09:18:46georg.brandlsetmessages: + msg119195
2010-10-20 08:52:51Retrosetmessages: + msg119194
2010-10-20 06:53:15georg.brandlsetfiles: - unnamed
2010-10-20 06:53:10georg.brandlsetmessages: + msg119191
2010-10-20 06:15:47ixokaisetnosy: + ixokai
messages: + msg119189
2010-10-19 18:55:24georg.brandlsetstatus: open -> closed
resolution: fixed
messages: + msg119158
2010-10-19 18:50:21Retrosetfiles: + unnamed

messages: + msg119157
2010-10-19 18:36:30eric.araujosetmessages: + msg119155
2010-10-19 18:29:03Retrosetnosy: + Retro
messages: + msg119153
2010-10-19 16:58:52eric.araujosetnosy: + eric.araujo
2010-10-17 13:54:46JJeffriessetnosy: + JJeffries
2010-10-13 20:04:32belopolskysetnosy: + twouters
2010-10-13 20:03:01belopolskysetnosy: + georg.brandl, christian.heimes
2010-10-13 19:53:35belopolskycreate