Commit e5a8df06 authored by Claude Paroz's avatar Claude Paroz
Browse files

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

Thanks Curtis Maloney for the report and the patch.
parent f565c6f9
Loading
Loading
Loading
Loading
+6 −7
Original line number Diff line number Diff line
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):