Skip to content
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

Closed
abalkin opened this issue Oct 13, 2010 · 19 comments
Closed

calendar does not restore locale properly #54301

abalkin opened this issue Oct 13, 2010 · 19 comments

Comments

@abalkin
Copy link
Member

abalkin commented Oct 13, 2010

BPO 10092
Nosy @Yhg1s, @birkenfeld, @tiran, @tjguk, @merwok, @bitdancer
Files
  • unnamed
  • 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:

    assignee = None
    closed_at = <Date 2010-10-19.18:55:24.743>
    created_at = <Date 2010-10-13.19:53:35.240>
    labels = []
    title = 'calendar does not restore locale properly'
    updated_at = <Date 2010-12-01.21:44:03.120>
    user = 'https://github.com/abalkin'

    bugs.python.org fields:

    activity = <Date 2010-12-01.21:44:03.120>
    actor = 'r.david.murray'
    assignee = 'none'
    closed = True
    closed_date = <Date 2010-10-19.18:55:24.743>
    closer = 'georg.brandl'
    components = []
    creation = <Date 2010-10-13.19:53:35.240>
    creator = 'belopolsky'
    dependencies = []
    files = ['19894']
    hgrepos = []
    issue_num = 10092
    keywords = []
    message_count = 19.0
    messages = ['118570', '119153', '119155', '119157', '119158', '119189', '119191', '119194', '119195', '119197', '119225', '119257', '119259', '119268', '123013', '123014', '123015', '123018', '123019']
    nosy_count = 9.0
    nosy_names = ['twouters', 'georg.brandl', 'ixokai', 'christian.heimes', 'tim.golden', 'eric.araujo', 'Retro', 'r.david.murray', 'JJeffries']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'test needed'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue10092'
    versions = []

    @abalkin
    Copy link
    Member Author

    abalkin commented Oct 13, 2010

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

    @Retro
    Copy link
    Mannequin

    Retro mannequin commented Oct 19, 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.

    @merwok
    Copy link
    Member

    merwok commented Oct 19, 2010

    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

    @Retro
    Copy link
    Mannequin

    Retro mannequin commented Oct 19, 2010

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


    @birkenfeld
    Copy link
    Member

    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.

    @ixokai
    Copy link
    Mannequin

    ixokai mannequin commented Oct 20, 2010

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

    @birkenfeld
    Copy link
    Member

    Let's see if r85735 fixed this.

    @Retro
    Copy link
    Mannequin

    Retro mannequin commented Oct 20, 2010

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

    @birkenfeld
    Copy link
    Member

    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.

    @tjguk
    Copy link
    Member

    tjguk commented Oct 20, 2010

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

    @Retro
    Copy link
    Mannequin

    Retro mannequin commented Oct 20, 2010

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


    @Retro
    Copy link
    Mannequin

    Retro mannequin commented Oct 21, 2010

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


    @bitdancer
    Copy link
    Member

    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.

    @tjguk
    Copy link
    Member

    tjguk commented Oct 21, 2010

    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?

    @Retro
    Copy link
    Mannequin

    Retro mannequin commented Dec 1, 2010

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

    @abalkin
    Copy link
    Member Author

    abalkin commented Dec 1, 2010

    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.

    @Retro
    Copy link
    Mannequin

    Retro mannequin commented Dec 1, 2010

    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

    @ixokai
    Copy link
    Mannequin

    ixokai mannequin commented Dec 1, 2010

    On windows, "France" may work and "fr_FR" may not; yet on OSX its exactly the opposite. Its not consistant across platforms.

    @bitdancer
    Copy link
    Member

    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.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    None yet
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants