Fixed #19846 -- Optimized a dict of lists in BlockContext class

Thanks Curtis Maloney for the report and the patch.
This commit is contained in:
Claude Paroz 2013-02-22 23:50:47 +01:00
parent f565c6f999
commit e5a8df06be
1 changed files with 6 additions and 7 deletions

View File

@ -1,3 +1,5 @@
from collections import defaultdict
from django.conf import settings
from django.template.base import TemplateSyntaxError, Library, Node, TextNode,\
token_kwargs, Variable
@ -15,19 +17,16 @@ class ExtendsError(Exception):
class BlockContext(object):
def __init__(self):
# Dictionary of FIFO queues.
self.blocks = {}
self.blocks = defaultdict(list)
def add_blocks(self, blocks):
for name, block in six.iteritems(blocks):
if name in self.blocks:
self.blocks[name].insert(0, block)
else:
self.blocks[name] = [block]
def pop(self, name):
try:
return self.blocks[name].pop()
except (IndexError, KeyError):
except IndexError:
return None
def push(self, name, block):
@ -36,7 +35,7 @@ class BlockContext(object):
def get_block(self, name):
try:
return self.blocks[name][-1]
except (IndexError, KeyError):
except IndexError:
return None
class BlockNode(Node):