detect custom async filters in as_const
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Richard Gibson · committed by David Lord · parent 27c6575 · Browse files · 3 files·+39 added−4 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+39 added−4 removed
- tests:+29 added−0 removed
- other:+10 added−4 removed
Provenance
- Commit
e1d0131- Tree
9327f58- Author
Richard Gibson <richard.gibson@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
27c6575
3 files changed+39 added−4 removed
modified:src/jinja2/nodes.py+7 added-4 removed
1 """AST nodes generated by the parser for the compiler. Also provides2 some node tree helper functions used by the parser and compiler in order3 to normalize nodes.4 """5+import inspect6 import operator7 from collections import deque8 from typing import Any9 from typing import Tuple as TupleType639 unmodified lines649 650 if filter_ is None or getattr(filter_, "contextfilter", False) is True:651 raise Impossible()652 652- # We cannot constant handle async filters, so we need to make sure653- # to not go down this path.654- if eval_ctx.environment.is_async and getattr(655- filter_, "asyncfiltervariant", False653+ # We cannot constant handle async filters, so we need to make654+ # sure to not go down this path. Account for both sync/async and655+ # pure-async filters.656+ if eval_ctx.environment.is_async and (657+ getattr(filter_, "asyncfiltervariant", False)658+ or inspect.iscoroutinefunction(filter_)659 ):660 raise Impossible()661 662 args, kwargs = args_as_const(self, eval_ctx)395 unmodified linesmodified:tests/test_asyncfilters.py+29 added
1 unmodified line2 3 import pytest4 5 from jinja2 import Environment6+from jinja2.asyncsupport import auto_aiter7 from jinja2.utils import Markup8 9 10 async def make_aiter(iter):211 unmodified lines222 assert out == (223 "[[0, 1, 2, 3], [4, 5, 6], [7, 8, 9]]|"224 "[[0, 1, 2, 3], [4, 5, 6, 'X'], [7, 8, 9, 'X']]"225 )226+227+228+def test_custom_async_filter(env_async):229+ async def customfilter(val):230+ return str(val)231+232+ env_async.filters["customfilter"] = customfilter233+ tmpl = env_async.from_string("{{ 'static'|customfilter }} {{ arg|customfilter }}")234+ out = tmpl.render(arg="dynamic")235+ assert out == "static dynamic"236+237+238+@mark_dualiter("items", lambda: range(10))239+def test_custom_async_iteratable_filter(env_async, items):240+ async def customfilter(iterable):241+ items = []242+ async for item in auto_aiter(iterable):243+ items.append(str(item))244+ if len(items) == 3:245+ break246+ return ",".join(items)247+248+ env_async.filters["customfilter"] = customfilter249+ tmpl = env_async.from_string(250+ "{{ items()|customfilter }} .. {{ [3, 4, 5, 6]|customfilter }}"251+ )252+ out = tmpl.render(items=items)253+ assert out == "0,1,2 .. 3,4,5"modified:CHANGES.rst+3 added
12 unmodified lines13 :class:`~loaders.PackageLoader`. :issue:`1168`14 - Fix a bug that caused imported macros to not have access to the15 current template's globals. :issue:`688`16 - Add ability to ignore ``trim_blocks`` using ``+%}``. :issue:`1036`17+- Fix a bug that caused custom async-only filters to fail with18+ constant input. :issue:`1279`19 20+21 Version 2.11.222 --------------23 24 Released 2020-04-13768 unmodified lines