Ticket #3465: 3465.diff

File 3465.diff, 2.6 KB (added by Gary Wilson <gary.wilson@…>, 8 years ago)

a patch with some regression tests

  • django/template/__init__.py

    === modified file 'django/template/__init__.py'
     
    665665                except (TypeError, AttributeError):
    666666                    try: # list-index lookup
    667667                        current = current[int(bits[0])]
    668                     except (IndexError, ValueError, KeyError):
     668                    except (IndexError, # list index out of range
     669                            ValueError, # invalid literal for int()
     670                            KeyError, # current is a dict without `int(bits[0])` key
     671                            TypeError, # unsubscriptable object
     672                            ):
    669673                        raise VariableDoesNotExist("Failed lookup for key [%s] in %r", (bits[0], current)) # missing attribute
    670674                except Exception, e:
    671675                    if getattr(e, 'silent_variable_failure', False):
  • tests/regressiontests/templates/tests.py

    === modified file 'tests/regressiontests/templates/tests.py'
     
    127127            # Fail silently when accessing a non-simple method
    128128            'basic-syntax20': ("{{ var.method2 }}", {"var": SomeClass()}, ("","INVALID")),
    129129
     130            # List-index syntax allows a template to access a certain item of a subscriptable object.
     131            'list-index01': ("{{ var.1 }}", {"var": ["first item", "second item"]}, "second item"),
     132
     133            # Fail silently when the list index is out of range.
     134            'list-index02': ("{{ var.5 }}", {"var": ["first item", "second item"]}, ("", "INVALID")),
     135
     136            # Fail silently when the variable is not a subscriptable object.
     137            'list-index03': ("{{ var.1 }}", {"var": None}, ("", "INVALID")),
     138
     139            # Fail silently when variable is a dict without the specified key.
     140            'list-index04': ("{{ var.1 }}", {"var": {}}, ("", "INVALID")),
     141
     142            # Dictionary lookup wins out when dict's key is a string.
     143            'list-index05': ("{{ var.1 }}", {"var": {'1': "hello"}}, "hello"),
     144
     145            # But list-index lookup wins out whin dict's key is an int.
     146            'list-index06': ("{{ var.1 }}", {"var": {1: "hello"}}, "hello"),
     147
     148            # Dictionary lookup wins out when there is a string and int version of the key.
     149            'list-index07': ("{{ var.1 }}", {"var": {'1': "hello", 1: "world"}}, "hello"),
     150           
    130151            # Basic filter usage
    131152            'basic-syntax21': ("{{ var|upper }}", {"var": "Django is the greatest!"}, "DJANGO IS THE GREATEST!"),
    132153
Back to Top