Avoided parallel assignment in template classes.

This commit is contained in:
Adam Johnson 2022-04-27 07:10:21 +01:00 committed by Mariusz Felisiak
parent 4a5753fb0a
commit 4e73d8c04d
4 changed files with 25 additions and 15 deletions

View File

@ -5,7 +5,9 @@ register = template.Library()
class AdminLogNode(template.Node): class AdminLogNode(template.Node):
def __init__(self, limit, varname, user): def __init__(self, limit, varname, user):
self.limit, self.varname, self.user = limit, varname, user self.limit = limit
self.varname = varname
self.user = user
def __repr__(self): def __repr__(self):
return "<GetAdminLog Node>" return "<GetAdminLog Node>"

View File

@ -307,7 +307,8 @@ class Token:
The line number the token appears on in the template source. The line number the token appears on in the template source.
This is used for traceback information and gettext files. This is used for traceback information and gettext files.
""" """
self.token_type, self.contents = token_type, contents self.token_type = token_type
self.contents = contents
self.lineno = lineno self.lineno = lineno
self.position = position self.position = position
@ -671,13 +672,12 @@ class FilterExpression:
"%s|%s|%s" % (token[:upto], token[upto:start], token[start:]) "%s|%s|%s" % (token[:upto], token[upto:start], token[start:])
) )
if var_obj is None: if var_obj is None:
var, constant = match["var"], match["constant"] if constant := match["constant"]:
if constant:
try: try:
var_obj = Variable(constant).resolve({}) var_obj = Variable(constant).resolve({})
except VariableDoesNotExist: except VariableDoesNotExist:
var_obj = None var_obj = None
elif var is None: elif (var := match["var"]) is None:
raise TemplateSyntaxError( raise TemplateSyntaxError(
"Could not find variable at start of %s." % token "Could not find variable at start of %s." % token
) )
@ -686,10 +686,9 @@ class FilterExpression:
else: else:
filter_name = match["filter_name"] filter_name = match["filter_name"]
args = [] args = []
constant_arg, var_arg = match["constant_arg"], match["var_arg"] if constant_arg := match["constant_arg"]:
if constant_arg:
args.append((False, Variable(constant_arg).resolve({}))) args.append((False, Variable(constant_arg).resolve({})))
elif var_arg: elif var_arg := match["var_arg"]:
args.append((True, Variable(var_arg))) args.append((True, Variable(var_arg)))
filter_func = parser.find_filter(filter_name) filter_func = parser.find_filter(filter_name)
self.args_check(filter_name, filter_func, args) self.args_check(filter_name, filter_func, args)

View File

@ -44,7 +44,8 @@ class AutoEscapeControlNode(Node):
"""Implement the actions of the autoescape tag.""" """Implement the actions of the autoescape tag."""
def __init__(self, setting, nodelist): def __init__(self, setting, nodelist):
self.setting, self.nodelist = setting, nodelist self.setting = setting
self.nodelist = nodelist
def render(self, context): def render(self, context):
old_setting = context.autoescape old_setting = context.autoescape
@ -129,7 +130,8 @@ class DebugNode(Node):
class FilterNode(Node): class FilterNode(Node):
def __init__(self, filter_expr, nodelist): def __init__(self, filter_expr, nodelist):
self.filter_expr, self.nodelist = filter_expr, nodelist self.filter_expr = filter_expr
self.nodelist = nodelist
def render(self, context): def render(self, context):
output = self.nodelist.render(context) output = self.nodelist.render(context)
@ -162,7 +164,8 @@ class ForNode(Node):
def __init__( def __init__(
self, loopvars, sequence, is_reversed, nodelist_loop, nodelist_empty=None self, loopvars, sequence, is_reversed, nodelist_loop, nodelist_empty=None
): ):
self.loopvars, self.sequence = loopvars, sequence self.loopvars = loopvars
self.sequence = sequence
self.is_reversed = is_reversed self.is_reversed = is_reversed
self.nodelist_loop = nodelist_loop self.nodelist_loop = nodelist_loop
if nodelist_empty is None: if nodelist_empty is None:
@ -249,7 +252,8 @@ class IfChangedNode(Node):
child_nodelists = ("nodelist_true", "nodelist_false") child_nodelists = ("nodelist_true", "nodelist_false")
def __init__(self, nodelist_true, nodelist_false, *varlist): def __init__(self, nodelist_true, nodelist_false, *varlist):
self.nodelist_true, self.nodelist_false = nodelist_true, nodelist_false self.nodelist_true = nodelist_true
self.nodelist_false = nodelist_false
self._varlist = varlist self._varlist = varlist
def render(self, context): def render(self, context):
@ -325,7 +329,9 @@ class IfNode(Node):
class LoremNode(Node): class LoremNode(Node):
def __init__(self, count, method, common): def __init__(self, count, method, common):
self.count, self.method, self.common = count, method, common self.count = count
self.method = method
self.common = common
def render(self, context): def render(self, context):
try: try:
@ -346,7 +352,8 @@ GroupedResult = namedtuple("GroupedResult", ["grouper", "list"])
class RegroupNode(Node): class RegroupNode(Node):
def __init__(self, target, expression, var_name): def __init__(self, target, expression, var_name):
self.target, self.expression = target, expression self.target = target
self.expression = expression
self.var_name = var_name self.var_name = var_name
def resolve_expression(self, obj, context): def resolve_expression(self, obj, context):

View File

@ -41,7 +41,9 @@ class BlockContext:
class BlockNode(Node): class BlockNode(Node):
def __init__(self, name, nodelist, parent=None): def __init__(self, name, nodelist, parent=None):
self.name, self.nodelist, self.parent = name, nodelist, parent self.name = name
self.nodelist = nodelist
self.parent = parent
def __repr__(self): def __repr__(self):
return "<Block Node: %s. Contents: %r>" % (self.name, self.nodelist) return "<Block Node: %s. Contents: %r>" % (self.name, self.nodelist)