[issue16322] time.tzname on Python 3.3.0 for Windows is decoded by wrong encoding

Petr Prikryl report at bugs.python.org
Sat Sep 19 20:34:55 CEST 2015


Petr Prikryl added the comment:

I have worked around a bit differently -- the snippet from the code:

    result = time.tzname[0]    # simplified version of the original code.

    # Because of the bug in Windows libraries, Python 3.3 tried to work around
    # some issues. However, the shit hit the fan, and the bug bubbled here.
    # The `time.tzname` elements are (unicode) strings; however, they were
    # filled with bad content. See https://bugs.python.org/issue16322 for details.
    # Actually, wrong characters were passed instead of the good ones.
    # This code should be skipped later by versions of Python that will fix
    # the issue.
    import platform
    if platform.system() == 'Windows':
        # The concrete example for Czech locale:
        # - cp1250 (windows-1250) is used as native encoding
        # - the time.tzname[0] should start with 'Střední Evropa'
        # - the ascii('Střední Evropa') should return "'St\u0159edn\xed Evropa'"
        # - because of the bug it returns "'St\xf8edn\xed Evropa'"
        #
        # The 'ř' character has unicode code point `\u0159` (that is hex)
        # and the `\xF8` code in cp1250. The `\xF8` was wrongly used
        # as a Unicode code point `\u00F8` -- this is for the Unicode
        # character 'ø' that is observed in the string.
        #
        # To fix it, the `result` string must be reinterpreted with a different
        # encoding. When working with Python 3 strings, it can probably
        # done only through the string representation and `eval()`. Here
        # the `eval()` is not very dangerous because the string was obtained
        # from the OS library, and the values are limited to certain subset.
        #
        # The `ascii()` literal is prefixed by `binary` type prefix character,
        # `eval`uated, and the binary result is decoded to the correct string.
        local_encoding = locale.getdefaultlocale()[1]
        b = eval('b' + ascii(result))
        result = b.decode(local_encoding)

----------

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


More information about the Python-bugs-list mailing list