Support old-style classes for attribute lookups. Fixes #631

This commit is contained in:
Armin Ronacher 2016-12-31 00:47:15 +01:00
parent e253520d96
commit 0ea221f53b
3 changed files with 10 additions and 1 deletions

View File

@ -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
-------------

View 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)

View 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