New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
calendar does not restore locale properly #54301
Comments
>>> 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' |
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. |
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 |
I'm not that geeky. :) On Tue, Oct 19, 2010 at 8:36 PM, Éric Araujo <report@bugs.python.org> wrote:
|
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. |
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: ====================================================================== 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. :) |
Let's see if r85735 fixed 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 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: 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. |
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. |
Boštjan, the code segment you quote is the *fallback* if the 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") 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'
>>> |
Thank you so much for your answer. The On Wed, Oct 20, 2010 at 11:37 AM, Tim Golden <report@bugs.python.org> wrote:
|
Please respond... On Wed, Oct 20, 2010 at 7:05 PM, Boštjan Mejak <report@bugs.python.org>wrote:
|
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. |
I'm afraid this falls outside my particular area of knowledge, and also |
>>> 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) |
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. |
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 |
On windows, "France" may work and "fr_FR" may not; yet on OSX its exactly the opposite. Its not consistant across platforms. |
Boštjan, please see bpo-10466 for further information about your question on fr_FR vs French. Windows, as usual, does not follow the standards. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: