Django

Code

Changeset 6969

Show
Ignore:
Timestamp:
12/22/07 13:10:03 (1 year ago)
Author:
adrian
Message:

Moved the various Debug classes in django.template to a new module, debug.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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/template/__init__.py

    r6778 r6969  
    225225        else: 
    226226            token = Token(TOKEN_TEXT, token_string) 
    227         return token 
    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 
    251227        return token 
    252228 
     
    361337            raise TemplateSyntaxError, "Invalid filter: '%s'" % filter_name 
    362338 
    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  
    401339def lexer_factory(*args, **kwargs): 
    402340    if settings.TEMPLATE_DEBUG: 
     341        from debug import DebugLexer 
    403342        return DebugLexer(*args, **kwargs) 
    404343    else: 
     
    407346def parser_factory(*args, **kwargs): 
    408347    if settings.TEMPLATE_DEBUG: 
     348        from debug import DebugParser 
    409349        return DebugParser(*args, **kwargs) 
    410350    else: 
     
    817757        return node.render(context) 
    818758 
    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  
    835759class TextNode(Node): 
    836760    def __init__(self, s): 
     
    861785        else: 
    862786            return force_unicode(output) 
    863  
    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 
    878787 
    879788def generic_tag_compiler(params, defaults, name, node_class, parser, token):