From e5a8df06be8ce82f5ba10dca5087339704ffd0fa Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Fri, 22 Feb 2013 23:50:47 +0100 Subject: [PATCH] Fixed #19846 -- Optimized a dict of lists in BlockContext class Thanks Curtis Maloney for the report and the patch. --- django/template/loader_tags.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/django/template/loader_tags.py b/django/template/loader_tags.py index d295d058d0..b0bf7139bf 100644 --- a/django/template/loader_tags.py +++ b/django/template/loader_tags.py @@ -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] + self.blocks[name].insert(0, 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):