mirror of
https://github.com/django/django.git
synced 2025-10-30 09:06:13 +00:00
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:
@@ -1,4 +1,4 @@
|
||||
from __future__ import absolute_import
|
||||
from __future__ import absolute_import, unicode_literals
|
||||
|
||||
import re
|
||||
from functools import partial
|
||||
@@ -836,7 +836,7 @@ class NodeList(list):
|
||||
else:
|
||||
bit = node
|
||||
bits.append(force_unicode(bit))
|
||||
return mark_safe(u''.join(bits))
|
||||
return mark_safe(''.join(bits))
|
||||
|
||||
def get_nodes_by_type(self, nodetype):
|
||||
"Return a list of all nodes of the given type"
|
||||
@@ -1006,8 +1006,8 @@ def parse_bits(parser, bits, params, varargs, varkw, defaults,
|
||||
if unhandled_params:
|
||||
# Some positional arguments were not supplied
|
||||
raise TemplateSyntaxError(
|
||||
u"'%s' did not receive value(s) for the argument(s): %s" %
|
||||
(name, u", ".join([u"'%s'" % p for p in unhandled_params])))
|
||||
"'%s' did not receive value(s) for the argument(s): %s" %
|
||||
(name, ", ".join(["'%s'" % p for p in unhandled_params])))
|
||||
return args, kwargs
|
||||
|
||||
def generic_tag_compiler(parser, token, params, varargs, varkw, defaults,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
"""Default variable filters."""
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import re
|
||||
import random as random_module
|
||||
@@ -140,14 +141,14 @@ def floatformat(text, arg=-1):
|
||||
input_val = force_unicode(text)
|
||||
d = Decimal(input_val)
|
||||
except UnicodeEncodeError:
|
||||
return u''
|
||||
return ''
|
||||
except InvalidOperation:
|
||||
if input_val in special_floats:
|
||||
return input_val
|
||||
try:
|
||||
d = Decimal(force_unicode(float(text)))
|
||||
except (ValueError, InvalidOperation, TypeError, UnicodeEncodeError):
|
||||
return u''
|
||||
return ''
|
||||
try:
|
||||
p = int(arg)
|
||||
except ValueError:
|
||||
@@ -159,12 +160,12 @@ def floatformat(text, arg=-1):
|
||||
return input_val
|
||||
|
||||
if not m and p < 0:
|
||||
return mark_safe(formats.number_format(u'%d' % (int(d)), 0))
|
||||
return mark_safe(formats.number_format('%d' % (int(d)), 0))
|
||||
|
||||
if p == 0:
|
||||
exp = Decimal(1)
|
||||
else:
|
||||
exp = Decimal(u'1.0') / (Decimal(10) ** abs(p))
|
||||
exp = Decimal('1.0') / (Decimal(10) ** abs(p))
|
||||
try:
|
||||
# Set the precision high enough to avoid an exception, see #15789.
|
||||
tupl = d.as_tuple()
|
||||
@@ -177,11 +178,11 @@ def floatformat(text, arg=-1):
|
||||
Context(prec=prec)).as_tuple()
|
||||
digits = [unicode(digit) for digit in reversed(digits)]
|
||||
while len(digits) <= abs(exponent):
|
||||
digits.append(u'0')
|
||||
digits.insert(-exponent, u'.')
|
||||
digits.append('0')
|
||||
digits.insert(-exponent, '.')
|
||||
if sign:
|
||||
digits.append(u'-')
|
||||
number = u''.join(reversed(digits))
|
||||
digits.append('-')
|
||||
number = ''.join(reversed(digits))
|
||||
return mark_safe(formats.number_format(number, abs(p)))
|
||||
except InvalidOperation:
|
||||
return input_val
|
||||
@@ -196,17 +197,17 @@ def iriencode(value):
|
||||
@stringfilter
|
||||
def linenumbers(value, autoescape=None):
|
||||
"""Displays text with line numbers."""
|
||||
lines = value.split(u'\n')
|
||||
lines = value.split('\n')
|
||||
# Find the maximum width of the line count, for use with zero padding
|
||||
# string format command
|
||||
width = unicode(len(unicode(len(lines))))
|
||||
if not autoescape or isinstance(value, SafeData):
|
||||
for i, line in enumerate(lines):
|
||||
lines[i] = (u"%0" + width + u"d. %s") % (i + 1, line)
|
||||
lines[i] = ("%0" + width + "d. %s") % (i + 1, line)
|
||||
else:
|
||||
for i, line in enumerate(lines):
|
||||
lines[i] = (u"%0" + width + u"d. %s") % (i + 1, escape(line))
|
||||
return mark_safe(u'\n'.join(lines))
|
||||
lines[i] = ("%0" + width + "d. %s") % (i + 1, escape(line))
|
||||
return mark_safe('\n'.join(lines))
|
||||
|
||||
@register.filter(is_safe=True)
|
||||
@stringfilter
|
||||
@@ -248,9 +249,9 @@ def stringformat(value, arg):
|
||||
of Python string formatting
|
||||
"""
|
||||
try:
|
||||
return (u"%" + unicode(arg)) % value
|
||||
return ("%" + unicode(arg)) % value
|
||||
except (ValueError, TypeError):
|
||||
return u""
|
||||
return ""
|
||||
|
||||
@register.filter(is_safe=True)
|
||||
@stringfilter
|
||||
@@ -394,7 +395,7 @@ def cut(value, arg):
|
||||
Removes all values of arg from the given string.
|
||||
"""
|
||||
safe = isinstance(value, SafeData)
|
||||
value = value.replace(arg, u'')
|
||||
value = value.replace(arg, '')
|
||||
if safe and arg != ';':
|
||||
return mark_safe(value)
|
||||
return value
|
||||
@@ -467,11 +468,11 @@ def safeseq(value):
|
||||
def removetags(value, tags):
|
||||
"""Removes a space separated list of [X]HTML tags from the output."""
|
||||
tags = [re.escape(tag) for tag in tags.split()]
|
||||
tags_re = u'(%s)' % u'|'.join(tags)
|
||||
starttag_re = re.compile(ur'<%s(/?>|(\s+[^>]*>))' % tags_re, re.U)
|
||||
endtag_re = re.compile(u'</%s>' % tags_re)
|
||||
value = starttag_re.sub(u'', value)
|
||||
value = endtag_re.sub(u'', value)
|
||||
tags_re = '(%s)' % '|'.join(tags)
|
||||
starttag_re = re.compile(r'<%s(/?>|(\s+[^>]*>))' % tags_re, re.U)
|
||||
endtag_re = re.compile('</%s>' % tags_re)
|
||||
value = starttag_re.sub('', value)
|
||||
value = endtag_re.sub('', value)
|
||||
return value
|
||||
|
||||
@register.filter(is_safe=True)
|
||||
@@ -493,7 +494,7 @@ def dictsort(value, arg):
|
||||
try:
|
||||
return sorted(value, key=Variable(arg).resolve)
|
||||
except (TypeError, VariableDoesNotExist):
|
||||
return u''
|
||||
return ''
|
||||
|
||||
@register.filter(is_safe=False)
|
||||
def dictsortreversed(value, arg):
|
||||
@@ -504,7 +505,7 @@ def dictsortreversed(value, arg):
|
||||
try:
|
||||
return sorted(value, key=Variable(arg).resolve, reverse=True)
|
||||
except (TypeError, VariableDoesNotExist):
|
||||
return u''
|
||||
return ''
|
||||
|
||||
@register.filter(is_safe=False)
|
||||
def first(value):
|
||||
@@ -512,7 +513,7 @@ def first(value):
|
||||
try:
|
||||
return value[0]
|
||||
except IndexError:
|
||||
return u''
|
||||
return ''
|
||||
|
||||
@register.filter(is_safe=True, needs_autoescape=True)
|
||||
def join(value, arg, autoescape=None):
|
||||
@@ -534,7 +535,7 @@ def last(value):
|
||||
try:
|
||||
return value[-1]
|
||||
except IndexError:
|
||||
return u''
|
||||
return ''
|
||||
|
||||
@register.filter(is_safe=True)
|
||||
def length(value):
|
||||
@@ -568,7 +569,7 @@ def slice_filter(value, arg):
|
||||
"""
|
||||
try:
|
||||
bits = []
|
||||
for x in arg.split(u':'):
|
||||
for x in arg.split(':'):
|
||||
if len(x) == 0:
|
||||
bits.append(None)
|
||||
else:
|
||||
@@ -635,7 +636,7 @@ def unordered_list(value, autoescape=None):
|
||||
second_item = new_second_item
|
||||
return [first_item, second_item], old_style_list
|
||||
def _helper(list_, tabs=1):
|
||||
indent = u'\t' * tabs
|
||||
indent = '\t' * tabs
|
||||
output = []
|
||||
|
||||
list_length = len(list_)
|
||||
@@ -708,7 +709,7 @@ def get_digit(value, arg):
|
||||
def date(value, arg=None):
|
||||
"""Formats a date according to the given format."""
|
||||
if not value:
|
||||
return u''
|
||||
return ''
|
||||
if arg is None:
|
||||
arg = settings.DATE_FORMAT
|
||||
try:
|
||||
@@ -722,8 +723,8 @@ def date(value, arg=None):
|
||||
@register.filter(expects_localtime=True, is_safe=False)
|
||||
def time(value, arg=None):
|
||||
"""Formats a time according to the given format."""
|
||||
if value in (None, u''):
|
||||
return u''
|
||||
if value in (None, ''):
|
||||
return ''
|
||||
if arg is None:
|
||||
arg = settings.TIME_FORMAT
|
||||
try:
|
||||
@@ -738,23 +739,23 @@ def time(value, arg=None):
|
||||
def timesince_filter(value, arg=None):
|
||||
"""Formats a date as the time since that date (i.e. "4 days, 6 hours")."""
|
||||
if not value:
|
||||
return u''
|
||||
return ''
|
||||
try:
|
||||
if arg:
|
||||
return timesince(value, arg)
|
||||
return timesince(value)
|
||||
except (ValueError, TypeError):
|
||||
return u''
|
||||
return ''
|
||||
|
||||
@register.filter("timeuntil", is_safe=False)
|
||||
def timeuntil_filter(value, arg=None):
|
||||
"""Formats a date as the time until that date (i.e. "4 days, 6 hours")."""
|
||||
if not value:
|
||||
return u''
|
||||
return ''
|
||||
try:
|
||||
return timeuntil(value, arg)
|
||||
except (ValueError, TypeError):
|
||||
return u''
|
||||
return ''
|
||||
|
||||
###################
|
||||
# LOGIC #
|
||||
@@ -795,7 +796,7 @@ def yesno(value, arg=None):
|
||||
"""
|
||||
if arg is None:
|
||||
arg = ugettext('yes,no,maybe')
|
||||
bits = arg.split(u',')
|
||||
bits = arg.split(',')
|
||||
if len(bits) < 2:
|
||||
return value # Invalid arg.
|
||||
try:
|
||||
@@ -839,7 +840,7 @@ def filesizeformat(bytes):
|
||||
return ugettext("%s PB") % filesize_number_format(bytes / (1024 * 1024 * 1024 * 1024 * 1024))
|
||||
|
||||
@register.filter(is_safe=False)
|
||||
def pluralize(value, arg=u's'):
|
||||
def pluralize(value, arg='s'):
|
||||
"""
|
||||
Returns a plural suffix if the value is not 1. By default, 's' is used as
|
||||
the suffix:
|
||||
@@ -862,11 +863,11 @@ def pluralize(value, arg=u's'):
|
||||
* If value is 1, cand{{ value|pluralize:"y,ies" }} displays "1 candy".
|
||||
* If value is 2, cand{{ value|pluralize:"y,ies" }} displays "2 candies".
|
||||
"""
|
||||
if not u',' in arg:
|
||||
arg = u',' + arg
|
||||
bits = arg.split(u',')
|
||||
if not ',' in arg:
|
||||
arg = ',' + arg
|
||||
bits = arg.split(',')
|
||||
if len(bits) > 2:
|
||||
return u''
|
||||
return ''
|
||||
singular_suffix, plural_suffix = bits[:2]
|
||||
|
||||
try:
|
||||
@@ -893,4 +894,4 @@ def pprint(value):
|
||||
try:
|
||||
return pformat(value)
|
||||
except Exception as e:
|
||||
return u"Error in formatting: %s" % force_unicode(e, errors="replace")
|
||||
return "Error in formatting: %s" % force_unicode(e, errors="replace")
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
"""Default tags used by the template system, available to all templates."""
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import sys
|
||||
import re
|
||||
@@ -13,7 +14,7 @@ from django.template.base import (Node, NodeList, Template, Context, Library,
|
||||
VARIABLE_ATTRIBUTE_SEPARATOR, get_library, token_kwargs, kwarg_re)
|
||||
from django.template.smartif import IfParser, Literal
|
||||
from django.template.defaultfilters import date
|
||||
from django.utils.encoding import smart_str, smart_unicode
|
||||
from django.utils.encoding import smart_unicode
|
||||
from django.utils.safestring import mark_safe
|
||||
from django.utils import timezone
|
||||
|
||||
@@ -43,9 +44,9 @@ class CsrfTokenNode(Node):
|
||||
csrf_token = context.get('csrf_token', None)
|
||||
if csrf_token:
|
||||
if csrf_token == 'NOTPROVIDED':
|
||||
return mark_safe(u"")
|
||||
return mark_safe("")
|
||||
else:
|
||||
return mark_safe(u"<div style='display:none'><input type='hidden' name='csrfmiddlewaretoken' value='%s' /></div>" % csrf_token)
|
||||
return mark_safe("<div style='display:none'><input type='hidden' name='csrfmiddlewaretoken' value='%s' /></div>" % csrf_token)
|
||||
else:
|
||||
# It's very probable that the token is missing because of
|
||||
# misconfiguration, so we raise a warning
|
||||
@@ -53,7 +54,7 @@ class CsrfTokenNode(Node):
|
||||
if settings.DEBUG:
|
||||
import warnings
|
||||
warnings.warn("A {% csrf_token %} was used in a template, but the context did not provide the value. This is usually caused by not using RequestContext.")
|
||||
return u''
|
||||
return ''
|
||||
|
||||
class CycleNode(Node):
|
||||
def __init__(self, cyclevars, variable_name=None, silent=False):
|
||||
@@ -102,7 +103,7 @@ class FirstOfNode(Node):
|
||||
value = var.resolve(context, True)
|
||||
if value:
|
||||
return smart_unicode(value)
|
||||
return u''
|
||||
return ''
|
||||
|
||||
class ForNode(Node):
|
||||
child_nodelists = ('nodelist_loop', 'nodelist_empty')
|
||||
@@ -390,7 +391,7 @@ class URLNode(Node):
|
||||
def render(self, context):
|
||||
from django.core.urlresolvers import reverse, NoReverseMatch
|
||||
args = [arg.resolve(context) for arg in self.args]
|
||||
kwargs = dict([(smart_str(k, 'ascii'), v.resolve(context))
|
||||
kwargs = dict([(smart_unicode(k, 'ascii'), v.resolve(context))
|
||||
for k, v in self.kwargs.items()])
|
||||
|
||||
view_name = self.view_name.resolve(context)
|
||||
@@ -486,7 +487,7 @@ def autoescape(parser, token):
|
||||
if len(args) != 2:
|
||||
raise TemplateSyntaxError("'autoescape' tag requires exactly one argument.")
|
||||
arg = args[1]
|
||||
if arg not in (u'on', u'off'):
|
||||
if arg not in ('on', 'off'):
|
||||
raise TemplateSyntaxError("'autoescape' argument should be 'on' or 'off'")
|
||||
nodelist = parser.parse(('endautoescape',))
|
||||
parser.delete_first_token()
|
||||
|
||||
Reference in New Issue
Block a user