Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

Thanks Curtis Maloney for the report and the patch.
  • Loading branch information...
commit e5a8df06be8ce82f5ba10dca5087339704ffd0fa 1 parent f565c6f
@claudep claudep authored
Showing with 6 additions and 7 deletions.
  1. +6 −7 django/template/loader_tags.py
View
13 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 @@ def push(self, name, block):
def get_block(self, name):
try:
return self.blocks[name][-1]
- except (IndexError, KeyError):
+ except IndexError:
return None
class BlockNode(Node):
Please sign in to comment.
Something went wrong with that request. Please try again.