Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Claude Paroz authored February 22, 2013

Showing 1 changed file with 6 additions and 7 deletions. Show diff stats Hide diff stats

  1. 13  django/template/loader_tags.py
13  django/template/loader_tags.py
... ...
@@ -1,3 +1,5 @@
  1
+from collections import defaultdict
  2
+
1 3
 from django.conf import settings
2 4
 from django.template.base import TemplateSyntaxError, Library, Node, TextNode,\
3 5
     token_kwargs, Variable
@@ -15,19 +17,16 @@ class ExtendsError(Exception):
15 17
 class BlockContext(object):
16 18
     def __init__(self):
17 19
         # Dictionary of FIFO queues.
18  
-        self.blocks = {}
  20
+        self.blocks = defaultdict(list)
19 21
 
20 22
     def add_blocks(self, blocks):
21 23
         for name, block in six.iteritems(blocks):
22  
-            if name in self.blocks:
23  
-                self.blocks[name].insert(0, block)
24  
-            else:
25  
-                self.blocks[name] = [block]
  24
+            self.blocks[name].insert(0, block)
26 25
 
27 26
     def pop(self, name):
28 27
         try:
29 28
             return self.blocks[name].pop()
30  
-        except (IndexError, KeyError):
  29
+        except IndexError:
31 30
             return None
32 31
 
33 32
     def push(self, name, block):
@@ -36,7 +35,7 @@ def push(self, name, block):
36 35
     def get_block(self, name):
37 36
         try:
38 37
             return self.blocks[name][-1]
39  
-        except (IndexError, KeyError):
  38
+        except IndexError:
40 39
             return None
41 40
 
42 41
 class BlockNode(Node):

0 notes on commit e5a8df0

Please sign in to comment.
Something went wrong with that request. Please try again.