Undefineds now support attribute errors for special attributes. This fixes #6
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 ab014bd · Browse files · 4 files·+23 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+23 added−6 removed
- other:+23 added−6 removed
Provenance
- Commit
6a3e95d- Tree
bde7034- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
ab014bd
4 files changed+23 added−6 removed
modified:jinja2/testsuite/api.py+8 added
178 unmodified lines179 assert t.render(test=test) == 'AB'180 t = Template('A{{ test().missingattribute }}B')181 self.assert_raises(UndefinedError, t.render, test=test)182 183+ def test_undefined_and_special_attributes(self):184+ try:185+ Undefined('Foo').__dict__186+ except AttributeError:187+ pass188+ else:189+ assert False, "Expected actual attribute error"190+191 def test_default_undefined(self):192 env = Environment(undefined=Undefined)193 self.assert_equal(env.from_string('{{ missing }}').render(), u'')194 self.assert_raises(UndefinedError,54 unmodified linesmodified:jinja2/testsuite/security.py+3 added-4 removed
54 unmodified lines55 foo=PublicStuff())56 self.assert_equal(env.from_string("{{ foo.bar() }}").render(foo=PublicStuff()), '23')57 self.assert_equal(env.from_string("{{ foo.__class__ }}").render(foo=42), '')58 self.assert_equal(env.from_string("{{ foo.func_code }}").render(foo=lambda:None), '')59+ # security error comes from __class__ already.60 self.assert_raises(SecurityError, env.from_string(61 "{{ foo.__class__.__subclasses__() }}").render, foo=42)62 63 def test_immutable_environment(self):44 unmodified lines108 assert escape('"<>&\'') == '"<>&''109 assert Markup("<em>Foo & Bar</em>").striptags() == "Foo & Bar"110 assert Markup("<test>").unescape() == "<test>"111 111-112 def test_template_data(self):113 env = Environment(autoescape=True)114 t = env.from_string('{% macro say_hello(name) %}'115 '<p>Hello {{ name }}!</p>{% endmacro %}'4 unmodified lines120 assert escape(t.module) == escaped_out121 assert t.module.say_hello('<blink>foo</blink>') == escaped_out122 assert escape(t.module.say_hello('<blink>foo</blink>')) == escaped_out123 124-124 def test_attr_filter(self):125 env = SandboxedEnvironment()127- tmpl = env.from_string('{{ 42|attr("__class__")|attr("__subclasses__")() }}')128- self.assert_raises(SecurityError, tmpl.render)126+ tmpl = env.from_string('{{ cls|attr("__subclasses__")() }}')127+ self.assert_raises(SecurityError, tmpl.render, cls=int)128 129 130 def suite():131 suite = unittest.TestSuite()2 unmodified linesmodified:jinja2/runtime.py+8 added-2 removed
456 unmodified lines457 else:458 hint = self._undefined_hint459 raise self._undefined_exception(hint)460 461+ @internalcode462+ def __getattr__(self, name):463+ if name[:2] == '__':464+ raise AttributeError(name)465+ return self._fail_with_undefined_error()466+467 __add__ = __radd__ = __mul__ = __rmul__ = __div__ = __rdiv__ = \468 __truediv__ = __rtruediv__ = __floordiv__ = __rfloordiv__ = \469 __mod__ = __rmod__ = __pos__ = __neg__ = __call__ = \464- __getattr__ = __getitem__ = __lt__ = __le__ = __gt__ = __ge__ = \465- __int__ = __float__ = __complex__ = __pow__ = __rpow__ = \470+ __getitem__ = __lt__ = __le__ = __gt__ = __ge__ = __int__ = \471+ __float__ = __complex__ = __pow__ = __rpow__ = \472 _fail_with_undefined_error473 474 def __str__(self):475 return unicode(self).encode('utf-8')75 unmodified linesmodified:CHANGES+4 added
3 unmodified lines4 Version 2.65 -----------6 (codename to be selected, release date to be announced)7 8+- internal attributes now raise an internal attribute error now instead9+ of returning an undefined. This fixes problems when passing undefined10+ objects to Python semantics expecting APIs.11+12 Version 2.5.513 -------------14 (re-release of 2.5.4 with built documentation removed for filesize.15 Released on October 18th 2010)228 unmodified lines