Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Moved the various Debug classes in django.template to a new module, d…

…ebug.py, so they're only loaded if DEBUG=True. This led to a DEBUG=False memory savings of one 4-KB memory block on my machine, according to ps

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6969 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 6557dc954b2d45b6e78104755b2c59347e4a05cc 1 parent c5d3a92
Adrian Holovaty authored December 22, 2007
95  django/template/__init__.py
@@ -226,30 +226,6 @@ def create_token(self, token_string, in_tag):
226 226
             token = Token(TOKEN_TEXT, token_string)
227 227
         return token
228 228
 
229  
-class DebugLexer(Lexer):
230  
-    def __init__(self, template_string, origin):
231  
-        super(DebugLexer, self).__init__(template_string, origin)
232  
-
233  
-    def tokenize(self):
234  
-        "Return a list of tokens from a given template_string"
235  
-        result, upto = [], 0
236  
-        for match in tag_re.finditer(self.template_string):
237  
-            start, end = match.span()
238  
-            if start > upto:
239  
-                result.append(self.create_token(self.template_string[upto:start], (upto, start), False))
240  
-                upto = start
241  
-            result.append(self.create_token(self.template_string[start:end], (start, end), True))
242  
-            upto = end
243  
-        last_bit = self.template_string[upto:]
244  
-        if last_bit:
245  
-            result.append(self.create_token(last_bit, (upto, upto + len(last_bit)), False))
246  
-        return result
247  
-
248  
-    def create_token(self, token_string, source, in_tag):
249  
-        token = super(DebugLexer, self).create_token(token_string, in_tag)
250  
-        token.source = self.origin, source
251  
-        return token
252  
-
253 229
 class Parser(object):
254 230
     def __init__(self, tokens):
255 231
         self.tokens = tokens
@@ -360,52 +336,16 @@ def find_filter(self, filter_name):
360 336
         else:
361 337
             raise TemplateSyntaxError, "Invalid filter: '%s'" % filter_name
362 338
 
363  
-class DebugParser(Parser):
364  
-    def __init__(self, lexer):
365  
-        super(DebugParser, self).__init__(lexer)
366  
-        self.command_stack = []
367  
-
368  
-    def enter_command(self, command, token):
369  
-        self.command_stack.append( (command, token.source) )
370  
-
371  
-    def exit_command(self):
372  
-        self.command_stack.pop()
373  
-
374  
-    def error(self, token, msg):
375  
-        return self.source_error(token.source, msg)
376  
-
377  
-    def source_error(self, source,msg):
378  
-        e = TemplateSyntaxError(msg)
379  
-        e.source = source
380  
-        return e
381  
-
382  
-    def create_nodelist(self):
383  
-        return DebugNodeList()
384  
-
385  
-    def create_variable_node(self, contents):
386  
-        return DebugVariableNode(contents)
387  
-
388  
-    def extend_nodelist(self, nodelist, node, token):
389  
-        node.source = token.source
390  
-        super(DebugParser, self).extend_nodelist(nodelist, node, token)
391  
-
392  
-    def unclosed_block_tag(self, parse_until):
393  
-        command, source = self.command_stack.pop()
394  
-        msg = "Unclosed tag '%s'. Looking for one of: %s " % (command, ', '.join(parse_until))
395  
-        raise self.source_error( source, msg)
396  
-
397  
-    def compile_function_error(self, token, e):
398  
-        if not hasattr(e, 'source'):
399  
-            e.source = token.source
400  
-
401 339
 def lexer_factory(*args, **kwargs):
402 340
     if settings.TEMPLATE_DEBUG:
  341
+        from debug import DebugLexer
403 342
         return DebugLexer(*args, **kwargs)
404 343
     else:
405 344
         return Lexer(*args, **kwargs)
406 345
 
407 346
 def parser_factory(*args, **kwargs):
408 347
     if settings.TEMPLATE_DEBUG:
  348
+        from debug import DebugParser
409 349
         return DebugParser(*args, **kwargs)
410 350
     else:
411 351
         return Parser(*args, **kwargs)
@@ -816,22 +756,6 @@ def get_nodes_by_type(self, nodetype):
816 756
     def render_node(self, node, context):
817 757
         return node.render(context)
818 758
 
819  
-class DebugNodeList(NodeList):
820  
-    def render_node(self, node, context):
821  
-        try:
822  
-            result = node.render(context)
823  
-        except TemplateSyntaxError, e:
824  
-            if not hasattr(e, 'source'):
825  
-                e.source = node.source
826  
-            raise
827  
-        except Exception, e:
828  
-            from sys import exc_info
829  
-            wrapped = TemplateSyntaxError('Caught an exception while rendering: %s' % e)
830  
-            wrapped.source = node.source
831  
-            wrapped.exc_info = exc_info()
832  
-            raise wrapped
833  
-        return result
834  
-
835 759
 class TextNode(Node):
836 760
     def __init__(self, s):
837 761
         self.s = s
@@ -861,21 +785,6 @@ def render(self, context):
861 785
         else:
862 786
             return force_unicode(output)
863 787
 
864  
-class DebugVariableNode(VariableNode):
865  
-    def render(self, context):
866  
-        try:
867  
-            output = force_unicode(self.filter_expression.resolve(context))
868  
-        except TemplateSyntaxError, e:
869  
-            if not hasattr(e, 'source'):
870  
-                e.source = self.source
871  
-            raise
872  
-        except UnicodeDecodeError:
873  
-            return ''
874  
-        if (context.autoescape and not isinstance(output, SafeData)) or isinstance(output, EscapeData):
875  
-            return escape(output)
876  
-        else:
877  
-            return output
878  
-
879 788
 def generic_tag_compiler(params, defaults, name, node_class, parser, token):
880 789
     "Returns a template.Node subclass."
881 790
     bits = token.split_contents()[1:]
97  django/template/debug.py
... ...
@@ -0,0 +1,97 @@
  1
+from django.template import Lexer, Parser, tag_re, NodeList, VariableNode, TemplateSyntaxError
  2
+from django.utils.encoding import force_unicode
  3
+from django.utils.html import escape
  4
+from django.utils.safestring import SafeData, EscapeData
  5
+
  6
+class DebugLexer(Lexer):
  7
+    def __init__(self, template_string, origin):
  8
+        super(DebugLexer, self).__init__(template_string, origin)
  9
+
  10
+    def tokenize(self):
  11
+        "Return a list of tokens from a given template_string"
  12
+        result, upto = [], 0
  13
+        for match in tag_re.finditer(self.template_string):
  14
+            start, end = match.span()
  15
+            if start > upto:
  16
+                result.append(self.create_token(self.template_string[upto:start], (upto, start), False))
  17
+                upto = start
  18
+            result.append(self.create_token(self.template_string[start:end], (start, end), True))
  19
+            upto = end
  20
+        last_bit = self.template_string[upto:]
  21
+        if last_bit:
  22
+            result.append(self.create_token(last_bit, (upto, upto + len(last_bit)), False))
  23
+        return result
  24
+
  25
+    def create_token(self, token_string, source, in_tag):
  26
+        token = super(DebugLexer, self).create_token(token_string, in_tag)
  27
+        token.source = self.origin, source
  28
+        return token
  29
+
  30
+class DebugParser(Parser):
  31
+    def __init__(self, lexer):
  32
+        super(DebugParser, self).__init__(lexer)
  33
+        self.command_stack = []
  34
+
  35
+    def enter_command(self, command, token):
  36
+        self.command_stack.append( (command, token.source) )
  37
+
  38
+    def exit_command(self):
  39
+        self.command_stack.pop()
  40
+
  41
+    def error(self, token, msg):
  42
+        return self.source_error(token.source, msg)
  43
+
  44
+    def source_error(self, source,msg):
  45
+        e = TemplateSyntaxError(msg)
  46
+        e.source = source
  47
+        return e
  48
+
  49
+    def create_nodelist(self):
  50
+        return DebugNodeList()
  51
+
  52
+    def create_variable_node(self, contents):
  53
+        return DebugVariableNode(contents)
  54
+
  55
+    def extend_nodelist(self, nodelist, node, token):
  56
+        node.source = token.source
  57
+        super(DebugParser, self).extend_nodelist(nodelist, node, token)
  58
+
  59
+    def unclosed_block_tag(self, parse_until):
  60
+        command, source = self.command_stack.pop()
  61
+        msg = "Unclosed tag '%s'. Looking for one of: %s " % (command, ', '.join(parse_until))
  62
+        raise self.source_error(source, msg)
  63
+
  64
+    def compile_function_error(self, token, e):
  65
+        if not hasattr(e, 'source'):
  66
+            e.source = token.source
  67
+
  68
+class DebugNodeList(NodeList):
  69
+    def render_node(self, node, context):
  70
+        try:
  71
+            result = node.render(context)
  72
+        except TemplateSyntaxError, e:
  73
+            if not hasattr(e, 'source'):
  74
+                e.source = node.source
  75
+            raise
  76
+        except Exception, e:
  77
+            from sys import exc_info
  78
+            wrapped = TemplateSyntaxError('Caught an exception while rendering: %s' % e)
  79
+            wrapped.source = node.source
  80
+            wrapped.exc_info = exc_info()
  81
+            raise wrapped
  82
+        return result
  83
+
  84
+class DebugVariableNode(VariableNode):
  85
+    def render(self, context):
  86
+        try:
  87
+            output = force_unicode(self.filter_expression.resolve(context))
  88
+        except TemplateSyntaxError, e:
  89
+            if not hasattr(e, 'source'):
  90
+                e.source = self.source
  91
+            raise
  92
+        except UnicodeDecodeError:
  93
+            return ''
  94
+        if (context.autoescape and not isinstance(output, SafeData)) or isinstance(output, EscapeData):
  95
+            return escape(output)
  96
+        else:
  97
+            return output

0 notes on commit 6557dc9

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