Skip to content
Myr

Improved internal async filter framework

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 a25669d · Browse files · 1 file+34 added−10 removed
This commit

State

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

Diff stats

1 file+34 added−10 removed

  • other:+34 added−10 removed

1 file+34 added−10 removed

modified:jinja2/asyncfilters.py+34 added-10 removed
12 unmodified lines
13 seq.append(item)14 return seq151617def dualfilter(normal_filter, async_filter):18 wrap_evalctx = False19 if getattr(normal_filter, 'environmentfilter'):20 is_async = lambda args: args[0].is_async21 wrap_evalctx = False22 else:23 if not getattr(normal_filter, 'evalcontextfilter'):24 wrap_evalctx = True25 is_async = lambda args: args[0].environment.is_async2627 @wraps(normal_filter)28 def wrapper(*args, **kwargs):29 b = is_async(args)30 if wrap_evalctx:31 args = args[1:]32 if b:33 return async_filter(*args, **kwargs)34 return normal_filter(*args, **kwargs)3536 if wrap_evalctx:37 wrapper.evalcontextfilter = True3839 return wrapper404142def asyncfiltervariant(original):43 def decorator(f):44 return dualfilter(original, f)45 return decorator464748@asyncfiltervariant(filters.do_first)17async def async_do_first(environment, seq):49async def do_first(environment, seq):50 try:51 return await auto_aiter(seq).__anext__()52 except StopAsyncIteration:53 return environment.undefined('No first item, sequence was empty.')545524@wraps(filters.do_first)25@filters.environmentfilter26def do_first(environment, seq):27 if environment.is_async:28 return async_do_first(environment, seq)29 return filters.do_first(environment, seq)303156ASYNC_FILTERS = {33 'first': do_first,57 'first': do_first,58}