From 5c3c470fc407101bd0bc9fac2654d1fc7dbd9998 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Fri, 12 Sep 2008 23:12:49 +0200 Subject: [PATCH] Fixed a bug in the subscript operation. --HG-- branch : trunk --- CHANGES | 4 ++++ jinja2/compiler.py | 7 ++----- tests/test_undefined.py | 8 ++++++++ 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/CHANGES b/CHANGES index c1eb733..eb730f1 100644 --- a/CHANGES +++ b/CHANGES @@ -14,6 +14,10 @@ Version 2.1 - fixed a limitation in the lexer that made ``{{ foo.0.0 }}`` impossible. +- index based subscribing of variables with a constant value returns an + undefined object now instead of raising an index error. This was a bug + caused by eager optimizing. + Version 2.0 ----------- (codename jinjavitus, released on July 17th 2008) diff --git a/jinja2/compiler.py b/jinja2/compiler.py index 4c0944a..6bd9428 100644 --- a/jinja2/compiler.py +++ b/jinja2/compiler.py @@ -1299,11 +1299,8 @@ class CodeGenerator(NodeVisitor): self.write(', %r)' % node.attr) def visit_Getitem(self, node, frame): - # slices or integer subscriptions bypass the getitem - # method if we can determine that at compile time. - if isinstance(node.arg, nodes.Slice) or \ - (isinstance(node.arg, nodes.Const) and - isinstance(node.arg.value, (int, long))): + # slices bypass the environment getitem method. + if isinstance(node.arg, nodes.Slice): self.visit(node.node, frame) self.write('[') self.visit(node.arg, frame) diff --git a/tests/test_undefined.py b/tests/test_undefined.py index f0544bb..eb1e7e6 100644 --- a/tests/test_undefined.py +++ b/tests/test_undefined.py @@ -6,6 +6,9 @@ :copyright: 2008 by Armin Ronacher. :license: BSD, see LICENSE for more details. """ +from py.test import raises +from jinja2 import Template +from jinja2.exceptions import UndefinedError test_default_undefined = ''' @@ -72,3 +75,8 @@ Traceback (most recent call last): ... UndefinedError: 'missing' is undefined ''' + + +def test_indexing_gives_undefined(): + t = Template("{{ var[42].foo }}") + raises(UndefinedError, t.render, var=0) -- 2.26.2