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 bebb94c15b
  Author: W. Trevor King <wking@tremily.us>
  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.
This commit is contained in:
W. Trevor King 2013-01-17 09:49:03 -05:00
commit d2a221c899

View file

@ -8,6 +8,7 @@ import logging
import functools import functools
import os import os
import re import re
import sys
from datetime import datetime from datetime import datetime
from sys import platform, stdin from sys import platform, stdin
@ -86,7 +87,10 @@ class Page(object):
self.date_format = settings['DEFAULT_DATE_FORMAT'] self.date_format = settings['DEFAULT_DATE_FORMAT']
if isinstance(self.date_format, tuple): 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] self.date_format = self.date_format[1]
if hasattr(self, 'date'): if hasattr(self, 'date'):