classification
Title: calendar throws UnicodeEncodeError when locale is specified
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.2, Python 3.3, Python 3.4, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: JJeffries, Retro, christian.heimes, eric.araujo, georg.brandl, ixokai, ned.deily, nneonneo, psam, python-dev, r.david.murray, rhettinger, serhiy.storchaka, tim.golden, twouters, vstinner
Priority: normal Keywords: patch

Created on 2013-01-27 08:48 by nneonneo, last changed 2013-01-31 14:17 by serhiy.storchaka. This issue is now closed.

Files
File name Uploaded Description Edit
calendar_locale-2.7.patch serhiy.storchaka, 2013-01-27 10:13 Patch for 2.7 review
calendar_locale_test-3.x.patch serhiy.storchaka, 2013-01-27 10:14 Test for 3.x review
Messages (8)
msg180750 - (view) Author: Robert Xiao (nneonneo) * Date: 2013-01-27 08:48
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>
    main(sys.argv)
  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)
msg180756 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-01-27 10:13
Thank you for report and suggestion, Robert Xiao. The patch should be a little more complex however.
msg180757 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-01-27 10:14
And here is an updated test for 3.x.
msg180786 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2013-01-27 20:11
See also Issue13539.
msg180865 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2013-01-28 17:42
Serhiy: not sure why all those people belong in the nosy list.
msg180866 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-01-28 17:46
They moved from issue13539 which I have closed as a duplicate.
msg181020 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-01-31 13:58
New changeset af41eca1959e by Serhiy Storchaka in branch '2.7':
Issue #17049: Localized calendar methods now return unicode if a locale
http://hg.python.org/cpython/rev/af41eca1959e
msg181022 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-01-31 14:17
Committed. Thank you for reports, Robert Xiao and psam.
History
Date User Action Args
2013-01-31 14:17:36serhiy.storchakasetstatus: open -> closed
resolution: fixed
messages: + msg181022

stage: patch review -> resolved
2013-01-31 13:58:35python-devsetnosy: + python-dev
messages: + msg181020
2013-01-28 17:46:56serhiy.storchakasetmessages: + msg180866
2013-01-28 17:42:24georg.brandlsetmessages: + msg180865
2013-01-28 17:11:18serhiy.storchakasetnosy: + twouters, georg.brandl, ixokai, vstinner, christian.heimes, tim.golden, eric.araujo, Retro, r.david.murray, JJeffries, psam
2013-01-28 17:10:14serhiy.storchakalinkissue13539 superseder
2013-01-27 20:11:11ned.deilysetnosy: + ned.deily
messages: + msg180786
2013-01-27 10:14:45serhiy.storchakasetfiles: + calendar_locale_test-3.x.patch

messages: + msg180757
versions: + Python 3.2, Python 3.3, Python 3.4
2013-01-27 10:13:43serhiy.storchakasetfiles: + calendar_locale-2.7.patch

type: behavior

keywords: + patch
nosy: + rhettinger, serhiy.storchaka
messages: + msg180756
stage: patch review
2013-01-27 08:48:46nneonneocreate