Django

Code

Ticket #7251: django-if-tag.2.patch

File django-if-tag.2.patch, 2.0 kB (added by Joshua 'jag' Ginsberg <jag@fsf.org>, 4 months ago)
  • template/defaulttags.py

    old new  
    5757        output.append(pformat(sys.modules)) 
    5858        return ''.join(output) 
    5959 
     60class ExceptionNode(Node): 
     61    def __init__(self, exception): 
     62        self.exception = exception 
     63 
     64    def render(self, context): 
     65        raise self.exception 
     66 
    6067class FilterNode(Node): 
    6168    def __init__(self, filter_expr, nodelist): 
    6269        self.filter_expr, self.nodelist = filter_expr, nodelist 
     
    640647    if len(bits) != 3: 
    641648        raise TemplateSyntaxError, "%r takes two arguments" % bits[0] 
    642649    end_tag = 'end' + bits[0] 
    643     nodelist_true = parser.parse(('else', end_tag)) 
     650    try: 
     651        nodelist_true = parser.parse(('else', end_tag)) 
     652    except Exception, e: 
     653        nodelist_true = NodeList([ExceptionNode(e)]) 
    644654    token = parser.next_token() 
    645655    if token.contents == 'else': 
    646         nodelist_false = parser.parse((end_tag,)) 
     656        try: 
     657            nodelist_false = parser.parse((end_tag,)) 
     658        except Exception, e: 
     659            nodelist_false = NodeList([ExceptionNode(e)]) 
    647660        parser.delete_first_token() 
    648661    else: 
    649662        nodelist_false = NodeList() 
     
    761774            boolvars.append((True, parser.compile_filter(boolvar))) 
    762775        else: 
    763776            boolvars.append((False, parser.compile_filter(boolpair))) 
    764     nodelist_true = parser.parse(('else', 'endif')) 
     777    try: 
     778        nodelist_true = parser.parse(('else', 'endif')) 
     779    except Exception, e: 
     780        nodelist_true = NodeList([ExceptionNode(e)]) 
    765781    token = parser.next_token() 
    766782    if token.contents == 'else': 
    767         nodelist_false = parser.parse(('endif',)) 
     783        try: 
     784            nodelist_false = parser.parse(('endif',)) 
     785        except Exception, e: 
     786            nodelist_false = NodeList([ExceptionNode(e)]) 
    768787        parser.delete_first_token() 
    769788    else: 
    770789        nodelist_false = NodeList()