mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	Fixed #19846 -- Optimized a dict of lists in BlockContext class
Thanks Curtis Maloney for the report and the patch.
This commit is contained in:
		| @@ -1,3 +1,5 @@ | |||||||
|  | from collections import defaultdict | ||||||
|  |  | ||||||
| from django.conf import settings | from django.conf import settings | ||||||
| from django.template.base import TemplateSyntaxError, Library, Node, TextNode,\ | from django.template.base import TemplateSyntaxError, Library, Node, TextNode,\ | ||||||
|     token_kwargs, Variable |     token_kwargs, Variable | ||||||
| @@ -15,19 +17,16 @@ class ExtendsError(Exception): | |||||||
| class BlockContext(object): | class BlockContext(object): | ||||||
|     def __init__(self): |     def __init__(self): | ||||||
|         # Dictionary of FIFO queues. |         # Dictionary of FIFO queues. | ||||||
|         self.blocks = {} |         self.blocks = defaultdict(list) | ||||||
|  |  | ||||||
|     def add_blocks(self, blocks): |     def add_blocks(self, blocks): | ||||||
|         for name, block in six.iteritems(blocks): |         for name, block in six.iteritems(blocks): | ||||||
|             if name in self.blocks: |             self.blocks[name].insert(0, block) | ||||||
|                 self.blocks[name].insert(0, block) |  | ||||||
|             else: |  | ||||||
|                 self.blocks[name] = [block] |  | ||||||
|  |  | ||||||
|     def pop(self, name): |     def pop(self, name): | ||||||
|         try: |         try: | ||||||
|             return self.blocks[name].pop() |             return self.blocks[name].pop() | ||||||
|         except (IndexError, KeyError): |         except IndexError: | ||||||
|             return None |             return None | ||||||
|  |  | ||||||
|     def push(self, name, block): |     def push(self, name, block): | ||||||
| @@ -36,7 +35,7 @@ class BlockContext(object): | |||||||
|     def get_block(self, name): |     def get_block(self, name): | ||||||
|         try: |         try: | ||||||
|             return self.blocks[name][-1] |             return self.blocks[name][-1] | ||||||
|         except (IndexError, KeyError): |         except IndexError: | ||||||
|             return None |             return None | ||||||
|  |  | ||||||
| class BlockNode(Node): | class BlockNode(Node): | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user