Skip to content
Myr

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

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent e253520 · Browse files · 3 files+10 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+10 added−1 removed

  • tests:+8 added−0 removed
  • other:+2 added−1 removed

3 files+10 added−1 removed

modified:jinja2/environment.py+1 added-1 removed
398 unmodified lines
399 def getitem(self, obj, argument):400 """Get an item or attribute of an object but prefer the item."""401 try:402 return obj[argument]403 except (TypeError, LookupError):403 except (AttributeError, TypeError, LookupError):404 if isinstance(argument, string_types):405 try:406 attr = str(argument)407 except Exception:
861 unmodified lines
modified:tests/test_regression.py+8 added
6 unmodified lines
78 :copyright: (c) 2010 by the Jinja Team.9 :license: BSD, see LICENSE for more details.10"""11import sys12import pytest1314from jinja2 import Template, Environment, DictLoader, TemplateSyntaxError, \15 TemplateNotFound, PrefixLoader
260 unmodified lines
276 output = tpl.render(callableclass=CallableClass(), hello='TEST')277 expected = 'TEST'278279 assert output == expected280281 @pytest.mark.skipif(sys.version_info[0] > 2,282 reason='This only works on 2.x')283 def test_old_style_attribute(self, env):284 class Foo:285 x = 42286 assert env.getitem(Foo(), 'x') == 42
modified:CHANGES+1 added
11 unmodified lines
12 not being what was expected.13- Added support for Python 3.6 async iterators through a new async mode.14- Added policies for filter defaults and similar things.15- urlize now sets "rel noopener" by default.16- Support attribute fallback for old-style classes in 2.x.1718Version 2.8.219-------------20
381 unmodified lines