items have a higher priority than attributes now. That's compatible with Jinja1 and Django
Before adoption: this commit came in with the history the project adopted, so no landing records it.
--HG-- branch : trunk
authored by Armin Ronacher · committed by Armin Ronacher · parent 61a5a24 · Browse files · 3 files·+38 added−31 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+38 added−31 removed
- tests:+10 added−0 removed
- other:+28 added−31 removed
Provenance
- Commit
f15f5f7- Tree
6f042d3- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
61a5a24
3 files changed+38 added−31 removed
modified:jinja2/environment.py+10 added-10 removed
286 unmodified lines287 return Lexer(self)288 289 def subscribe(self, obj, argument):290 """Get an item or attribute of an object."""291- if isinstance(argument, basestring):292- try:293- attr = str(argument)294- except:295- pass296- else:297- try:298- return getattr(obj, attr)299- except AttributeError:300- pass291 try:292 return obj[argument]293 except (TypeError, LookupError):294+ if isinstance(argument, basestring):295+ try:296+ attr = str(argument)297+ except:298+ pass299+ else:300+ try:301+ return getattr(obj, attr)302+ except AttributeError:303+ pass304 return self.undefined(obj=obj, name=argument)305 306 def parse(self, source, name=None, filename=None):307 """Parse the sourcecode and return the abstract syntax tree. This381 unmodified linesmodified:jinja2/sandbox.py+18 added-21 removed
162 unmodified lines163 getattr(obj, 'alters_data', False))164 165 def subscribe(self, obj, argument):166 """Subscribe an object from sandboxed code."""167- is_unsafe = False168- if isinstance(argument, basestring):169- try:170- attr = str(argument)171- except:172- pass173- else:174- try:175- value = getattr(obj, attr)176- except AttributeError:177- pass178- else:179- if self.is_safe_attribute(obj, argument, value):180- return value181- is_unsafe = True167 try:168 return obj[argument]169 except (TypeError, LookupError):170+ if isinstance(argument, basestring):171+ try:172+ attr = str(argument)173+ except:174+ pass175+ else:176+ try:177+ value = getattr(obj, attr)178+ except AttributeError:179+ pass180+ else:181+ if self.is_safe_attribute(obj, argument, value):185- if is_unsafe:186- return self.undefined('access to attribute %r of %r object is'187- ' unsafe.' % (188- argument,189- obj.__class__.__name__190- ), name=argument, exc=SecurityError)182+ return value183+ return self.undefined('access to attribute %r of %r '184+ 'object is unsafe.' % (185+ argument,186+ obj.__class__.__name__187+ ), name=argument, exc=SecurityError)188 return self.undefined(obj=obj, name=argument)189 190 def call(__self, __context, __obj, *args, **kwargs):191 """Call an object from sandboxed code."""17 unmodified linesmodified:tests/test_various.py+10 added
56 unmodified lines57 escape(u"foo")58 escape(u"<foo>")59 counts.add(len(gc.get_objects()))60 assert len(counts) == 1, 'ouch, c extension seems to leak objects'61+62+63+def test_item_before_attribute():64+ from jinja2 import Environment65+ from jinja2.sandbox import SandboxedEnvironment66+67+ for env in Environment(), SandboxedEnvironment():68+ tmpl = env.from_string('{{ foo.items() }}')69+ assert tmpl.render(foo={'items': lambda: 42}) == '42'70+ assert tmpl.render(foo={}) == '[]'