Ticket #11421: 11421.patch

File 11421.patch, 3.4 KB (added by Peter Bengtsson, 15 years ago)
  • django/template/__init__.py

     
    139139class InvalidTemplateLibrary(Exception):
    140140    pass
    141141
     142class AttributeLookupError(Exception):
     143    pass
     144
    142145class Origin(object):
    143146    def __init__(self, name):
    144147        self.name = name
     
    713716                current = current[bit]
    714717            except (TypeError, AttributeError, KeyError):
    715718                try: # attribute lookup
    716                     current = getattr(current, bit)
     719                    try:
     720                        current = getattr(current, bit)
     721                    except AttributeError, e:
     722                        raise AttributeLookupError(e)
    717723                    if callable(current):
    718724                        if getattr(current, 'alters_data', False):
    719725                            current = settings.TEMPLATE_STRING_IF_INVALID
     
    724730                                # GOTCHA: This will also catch any TypeError
    725731                                # raised in the function itself.
    726732                                current = settings.TEMPLATE_STRING_IF_INVALID # invalid method call
     733                            except AttributeError:
     734                                raise
    727735                            except Exception, e:
    728736                                if getattr(e, 'silent_variable_failure', False):
    729737                                    current = settings.TEMPLATE_STRING_IF_INVALID
    730738                                else:
    731739                                    raise
    732                 except (TypeError, AttributeError):
     740                except (TypeError, AttributeLookupError):
    733741                    try: # list-index lookup
    734742                        current = current[int(bit)]
    735743                    except (IndexError, # list index out of range
  • tests/regressiontests/templates/tests.py

     
    8888
    8989    def method4(self):
    9090        raise SomeOtherException
     91   
     92    def method5(self):
     93        raise AttributeError("callable exists but raises this")
    9194
    9295class OtherClass:
    9396    def method(self):
     
    303306
    304307            # Fail silently when a variable's attribute isn't found
    305308            'basic-syntax11': ("{{ var.blech }}", {"var": SomeClass()}, ("","INVALID")),
    306 
     309               
    307310            # Raise TemplateSyntaxError when trying to access a variable beginning with an underscore
    308311            'basic-syntax12': ("{{ var.__dict__ }}", {"var": SomeClass()}, template.TemplateSyntaxError),
    309312
     
    340343            'basic-syntax25': ('{{ "fred" }}', {}, "fred"),
    341344            'basic-syntax26': (r'{{ "\"fred\"" }}', {}, "\"fred\""),
    342345            'basic-syntax27': (r'{{ _("\"fred\"") }}', {}, "\"fred\""),
     346               
     347            # Don't fail silently if variable did exist but raised an
     348            # AttributeError on execution
     349            'basic-syntax28': ("{{ var.method5 }}", {"var": SomeClass()}, AttributeError),
    343350
    344351            # List-index syntax allows a template to access a certain item of a subscriptable object.
    345352            'list-index01': ("{{ var.1 }}", {"var": ["first item", "second item"]}, "second item"),
Back to Top