]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Support old-style classes for attribute lookups. Fixes #631
authorArmin Ronacher <armin.ronacher@active-4.com>
Fri, 30 Dec 2016 23:47:15 +0000 (00:47 +0100)
committerArmin Ronacher <armin.ronacher@active-4.com>
Fri, 30 Dec 2016 23:47:15 +0000 (00:47 +0100)
CHANGES
jinja2/environment.py
tests/test_regression.py

diff --git a/CHANGES b/CHANGES
index aac02effbe309f36f4a100ba3813fd206c7fcd5f..e92c3719f993bbd49c539f5f5707cf8b5f37c37b 100644 (file)
--- 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
 -------------
index 1aff8f54d726f136d10d22f1e556654083317855..cfa7ff1e1bd906fff64f6c444e89607d36834010 100644 (file)
@@ -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)
index 7b18ef42c3745adfea294a9844560e602b8b150a..cd46c8c85f19d23ff7f60f9a733d7e62f7c90505 100644 (file)
@@ -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