[issue10092] calendar does not restore locale properly

Boštjan Mejak report at bugs.python.org
Wed Oct 20 10:52:52 CEST 2010


Boštjan Mejak <bostjan.mejak at gmail.com> added the comment:

>>> 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.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue10092>
_______________________________________


More information about the Python-bugs-list mailing list