From d2a221c8998dff7adb4baa75c4c0f2953187cb7f Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Thu, 17 Jan 2013 09:49:03 -0500 Subject: [PATCH] contents: Encode Unicode locales for Python 2 in Page.__init__ Python 2.7 chokes on Unicode locales: $ python2.7 >>> import locale >>> locale.setlocale(locale.LC_ALL, u'ja_JP.utf8') Traceback (most recent call last): ... ValueError: too many values to unpack With the addition of: from __future__ import unicode_literals to tests/test_contents.py in: commit bebb94c15be1ae7ff7ec241f6e1de5aa18556654 Author: W. Trevor King Date: Tue Jan 15 22:50:58 2013 -0500 test_contents.py: Add URLWrapper comparison tests the locale strings in TestPage.test_datetime are interpreted as Unicode. Rather than fixing the encoding there, this patch updates Page to handle Unicode locales automatically. --- pelican/contents.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pelican/contents.py b/pelican/contents.py index bd257ad8..dc38e32f 100644 --- a/pelican/contents.py +++ b/pelican/contents.py @@ -8,6 +8,7 @@ import logging import functools import os import re +import sys from datetime import datetime from sys import platform, stdin @@ -86,7 +87,10 @@ class Page(object): self.date_format = settings['DEFAULT_DATE_FORMAT'] if isinstance(self.date_format, tuple): - locale.setlocale(locale.LC_ALL, self.date_format[0]) + locale_string = self.date_format[0] + if sys.version_info < (3, ) and isinstance(locale_string, six.text_type): + locale_string = locale_string.encode('ascii') + locale.setlocale(locale.LC_ALL, locale_string) self.date_format = self.date_format[1] if hasattr(self, 'date'):