[New-bugs-announce] [issue17049] calendar throws UnicodeEncodeError when locale is specified

Robert Xiao report at bugs.python.org
Sun Jan 27 09:48:46 CET 2013

New submission from Robert Xiao:

Try this at a command-prompt:

$ python -m calendar -L ja_JP --encoding utf8

The result is a crash:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/calendar.py", line 708, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/calendar.py", line 703, in main
    result = result.encode(options.encoding)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 49: ordinal not in range(128)

The reason is because the TimeEncoding class doesn't properly return the encoding it should use, so 'encoding' ends up being None in "with TimeEncoding(self.locale) as encoding:" lines.

Trivial patch:

--- calendar.py	2013-01-27 03:48:08.000000000 -0500
+++ calendar.py	2013-01-27 03:48:08.000000000 -0500
@@ -488,6 +488,7 @@
     def __enter__(self):
         self.oldlocale = _locale.getlocale(_locale.LC_TIME)
         _locale.setlocale(_locale.LC_TIME, self.locale)
+        return self.locale[1]
     def __exit__(self, *args):
         _locale.setlocale(_locale.LC_TIME, self.oldlocale)

components: Library (Lib)
messages: 180750
nosy: nneonneo
priority: normal
severity: normal
status: open
title: calendar throws UnicodeEncodeError when locale is specified
versions: Python 2.7

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list