Fixed async macro autoescape. Fixes #671
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 6295976 · Browse files · 4 files·+38 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
4 files+38 added−1 removed
- tests:+15 added−0 removed
- other:+23 added−1 removed
Provenance
- Commit
add7916- Tree
4e33d38- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
6295976
4 files changed+38 added−1 removed
modified:jinja2/asyncsupport.py+18 added-1 removed
94 unmodified lines95 96 return update_wrapper(__call__, original_call)97 98 99+def wrap_macro_invoke(original_invoke):100+ @internalcode101+ async def async_invoke(self, arguments, autoescape):102+ rv = await self._func(*arguments)103+ if autoescape:104+ rv = Markup(rv)105+ return rv106+107+ @internalcode108+ def _invoke(self, arguments, autoescape):109+ if not self._environment.is_async:110+ return original_invoke(self, arguments, autoescape)111+ return async_invoke(self, arguments, autoescape)112+ return update_wrapper(_invoke, original_invoke)113+114+115 @internalcode116 async def get_default_module_async(self):117 if self._module is not None:118 return self._module34 unmodified lines153 make_module_async, Template.make_module_async)154 155 156 def patch_runtime():141- from jinja2.runtime import BlockReference157+ from jinja2.runtime import BlockReference, Macro158 BlockReference.__call__ = wrap_block_reference_call(159 BlockReference.__call__)160+ Macro._invoke = wrap_macro_invoke(Macro._invoke)161 162 163 def patch_filters():164 from jinja2.filters import FILTERS90 unmodified linesmodified:jinja2/runtime.py+4 added
542 unmodified lines543 elif len(args) > self._argument_count:544 raise TypeError('macro %r takes not more than %d argument(s)' %545 (self.name, len(self.arguments)))546 547+ return self._invoke(arguments, autoescape)548+549+ def _invoke(self, arguments, autoescape):550+ """This method is being swapped out by the async implementation."""551 rv = self._func(*arguments)552 if autoescape:553 rv = Markup(rv)554 return rv231 unmodified linesmodified:tests/test_async.py+15 added
241 unmodified lines242 {{ outer("FOO") }}243 """)244 assert t.render().strip() == '(FOO)'245 246+ def test_unoptimized_scopes_autoescape(self):247+ env = Environment(loader=DictLoader(dict(248+ o_printer='({{ o }})',249+ )), autoescape=True, enable_async=True)250+ t = env.from_string("""251+ {% macro outer(o) %}252+ {% macro inner() %}253+ {% include "o_printer" %}254+ {% endmacro %}255+ {{ inner() }}256+ {% endmacro %}257+ {{ outer("FOO") }}258+ """)259+ assert t.render().strip() == '(FOO)'260 261+262 @pytest.mark.core_tags263 @pytest.mark.for_loop264 class TestAsyncForLoop(object):265 220 unmodified linesmodified:CHANGES+1 added
15 unmodified lines16 - Resolve an issue where the `{% extends %}` tag could not be used with17 async environments. (#668)18 - Reduce memory footprint slightly by reducing our unicode database dump19 we use for identifier matching on Python 3 (#666)20+- Fixed autoescaping not working for macros in async compilation mode. (#671)21 22 Version 2.9.423 -------------24 (bugfix release, released on January 10th 2017)480 unmodified lines