From: Armin Ronacher Date: Fri, 30 Dec 2016 23:47:15 +0000 (+0100) Subject: Support old-style classes for attribute lookups. Fixes #631 X-Git-Tag: 2.9~37 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0ea221f53b2df44b9d02aec00ff7f87424971d4e;p=thirdparty%2Fjinja.git Support old-style classes for attribute lookups. Fixes #631 --- diff --git a/CHANGES b/CHANGES index aac02eff..e92c3719 100644 --- a/CHANGES +++ b/CHANGES @@ -13,6 +13,7 @@ Version 2.9 - Added support for Python 3.6 async iterators through a new async mode. - Added policies for filter defaults and similar things. - urlize now sets "rel noopener" by default. +- Support attribute fallback for old-style classes in 2.x. Version 2.8.2 ------------- diff --git a/jinja2/environment.py b/jinja2/environment.py index 1aff8f54..cfa7ff1e 100644 --- a/jinja2/environment.py +++ b/jinja2/environment.py @@ -400,7 +400,7 @@ class Environment(object): """Get an item or attribute of an object but prefer the item.""" try: return obj[argument] - except (TypeError, LookupError): + except (AttributeError, TypeError, LookupError): if isinstance(argument, string_types): try: attr = str(argument) diff --git a/tests/test_regression.py b/tests/test_regression.py index 7b18ef42..cd46c8c8 100644 --- a/tests/test_regression.py +++ b/tests/test_regression.py @@ -8,6 +8,7 @@ :copyright: (c) 2010 by the Jinja Team. :license: BSD, see LICENSE for more details. """ +import sys import pytest from jinja2 import Template, Environment, DictLoader, TemplateSyntaxError, \ @@ -276,3 +277,10 @@ class TestBug(): expected = 'TEST' assert output == expected + + @pytest.mark.skipif(sys.version_info[0] > 2, + reason='This only works on 2.x') + def test_old_style_attribute(self, env): + class Foo: + x = 42 + assert env.getitem(Foo(), 'x') == 42