Fixed #18269 -- Applied unicode_literals for Python 3 compatibility.

Thanks Vinay Sajip for the support of his django3 branch and
Jannis Leidel for the review.
This commit is contained in:
Claude Paroz 2012-06-07 18:08:47 +02:00
parent 706fd9adc0
commit 4a103086d5
401 changed files with 6647 additions and 6157 deletions

View file

@ -1,3 +1,5 @@
from __future__ import unicode_literals
import re
import unicodedata
import warnings
@ -43,7 +45,7 @@ def wrap(text, width):
if len(lines) > 1:
pos = len(lines[-1])
yield word
return u''.join(_generator())
return ''.join(_generator())
wrap = allow_lazy(wrap, unicode)
@ -58,7 +60,7 @@ class Truncator(SimpleLazyObject):
if truncate is None:
truncate = pgettext(
'String to return when truncating text',
u'%(truncated_text)s...')
'%(truncated_text)s...')
truncate = force_unicode(truncate)
if '%(truncated_text)s' in truncate:
return truncate % {'truncated_text': text}
@ -130,8 +132,8 @@ class Truncator(SimpleLazyObject):
words = self._wrapped.split()
if len(words) > length:
words = words[:length]
return self.add_truncation_text(u' '.join(words), truncate)
return u' '.join(words)
return self.add_truncation_text(' '.join(words), truncate)
return ' '.join(words)
def _html_words(self, length, truncate):
"""
@ -142,7 +144,7 @@ class Truncator(SimpleLazyObject):
Newlines in the HTML are preserved.
"""
if length <= 0:
return u''
return ''
html4_singlets = (
'br', 'col', 'link', 'base', 'img',
'param', 'area', 'hr', 'input'
@ -221,28 +223,28 @@ def get_valid_filename(s):
spaces are converted to underscores; and anything that is not a unicode
alphanumeric, dash, underscore, or dot, is removed.
>>> get_valid_filename("john's portrait in 2004.jpg")
u'johns_portrait_in_2004.jpg'
'johns_portrait_in_2004.jpg'
"""
s = force_unicode(s).strip().replace(' ', '_')
return re.sub(r'(?u)[^-\w.]', '', s)
get_valid_filename = allow_lazy(get_valid_filename, unicode)
def get_text_list(list_, last_word=ugettext_lazy(u'or')):
def get_text_list(list_, last_word=ugettext_lazy('or')):
"""
>>> get_text_list(['a', 'b', 'c', 'd'])
u'a, b, c or d'
'a, b, c or d'
>>> get_text_list(['a', 'b', 'c'], 'and')
u'a, b and c'
'a, b and c'
>>> get_text_list(['a', 'b'], 'and')
u'a and b'
'a and b'
>>> get_text_list(['a'])
u'a'
'a'
>>> get_text_list([])
u''
''
"""
if len(list_) == 0: return u''
if len(list_) == 0: return ''
if len(list_) == 1: return force_unicode(list_[0])
return u'%s %s %s' % (
return '%s %s %s' % (
# Translators: This string is used as a separator between list elements
_(', ').join([force_unicode(i) for i in list_][:-1]),
force_unicode(last_word), force_unicode(list_[-1]))
@ -267,7 +269,7 @@ def phone2numeric(phone):
'n': '6', 'o': '6', 'p': '7', 'q': '7', 'r': '7', 's': '7', 't': '8',
'u': '8', 'v': '8', 'w': '9', 'x': '9', 'y': '9', 'z': '9',
}
return u''.join(char2number.get(c, c) for c in phone.lower())
return ''.join(char2number.get(c, c) for c in phone.lower())
phone2numeric = allow_lazy(phone2numeric)
# From http://www.xhaus.com/alan/python/httpcomp.html#gzip
@ -279,12 +281,12 @@ def compress_string(s):
zfile.close()
return zbuf.getvalue()
ustring_re = re.compile(u"([\u0080-\uffff])")
ustring_re = re.compile("([\u0080-\uffff])")
def javascript_quote(s, quote_double_quotes=False):
def fix(match):
return r"\u%04x" % ord(match.group(1))
return b"\u%04x" % ord(match.group(1))
if type(s) == str:
s = s.decode('utf-8')
@ -321,11 +323,11 @@ def smart_split(text):
be further processed with unescape_string_literal()).
>>> list(smart_split(r'This is "a person\'s" test.'))
[u'This', u'is', u'"a person\\\'s"', u'test.']
['This', 'is', '"a person\\\'s"', 'test.']
>>> list(smart_split(r"Another 'person\'s' test."))
[u'Another', u"'person\\'s'", u'test.']
['Another', "'person\\'s'", 'test.']
>>> list(smart_split(r'A "\"funky\" style" test.'))
[u'A', u'"\\"funky\\" style"', u'test.']
['A', '"\\"funky\\" style"', 'test.']
"""
text = force_unicode(text)
for bit in smart_split_re.finditer(text):
@ -334,10 +336,10 @@ smart_split = allow_lazy(smart_split, unicode)
def _replace_entity(match):
text = match.group(1)
if text[0] == u'#':
if text[0] == '#':
text = text[1:]
try:
if text[0] in u'xX':
if text[0] in 'xX':
c = int(text[1:], 16)
else:
c = int(text)