Added basic support for async iter
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 3e6992f · Browse files · 3 files·+34 added−2 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+34 added−2 removed
- tests:+11 added−0 removed
- other:+23 added−2 removed
Provenance
- Commit
aa188fd- Tree
d579367- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
3e6992f
3 files changed+34 added−2 removed
modified:jinja2/asyncsupport.py+9 added
133 unmodified lines134 async def auto_await(value):135 if inspect.isawaitable(value):136 return await value137 return value138+139+140+async def auto_iter(iterable):141+ if hasattr(iterable, '__aiter__'):142+ async for item in iterable:143+ yield item144+ return145+ for item in iterable:146+ yield itemmodified:jinja2/compiler.py+14 added-2 removed
781 unmodified lines782 if not unoptimize_before_dead_code:783 self.writeline('dummy = lambda *x: None')784 785 if self.environment._async:786- self.writeline('from jinja2.asyncsupport import auto_await')786+ self.writeline('from jinja2.asyncsupport import auto_await, auto_iter')787 788 # if we want a deferred initialization we cannot move the789 # environment into a local name790 envenv = not self.defer_init and ', environment=environment' or ''338 unmodified lines1129 " variable of the current loop. Because there is no parent "1130 "loop it's undefined. Happened in loop on %s" %1131 self.position(node)))1132 1133- self.writeline('for ', node)1133+ self.writeline(self.environment._async and 'async for ' or 'for ', node)1134 self.visit(node.target, loop_frame)1135 self.write(extended_loop and ', l_loop in LoopContext(' or ' in ')1136 1137 # if we have an extened loop and a node test, we filter in the1138 # "outer frame".1139 if extended_loop and node.test is not None:1140+ if self.environment._async:1141+ self.fail('loop filters in async mode are currently if the '1142+ 'loop uses the special "loop" variable or is '1143+ 'recursive.', node.lineno)1144 self.write('(')1145 self.visit(node.target, loop_frame)1146 self.write(' for ')1147 self.visit(node.target, loop_frame)9 unmodified lines1157 1158 elif node.recursive:1159 self.write('reciter')1160 else:1161+ if self.environment._async:1162+ self.write('auto_iter(')1163 self.visit(node.iter, loop_frame)1164+ if self.environment._async:1165+ self.write(')')1166 1167 if node.recursive:1168 self.write(', loop_render_func, depth):')1169 else:31 unmodified lines1201 self.return_buffer_contents(loop_frame)1202 self.outdent()1203 self.start_write(frame, node)1204 self.write('loop(')1205+ if self.environment._async:1206+ self.write('auto_iter(')1207 self.visit(node.iter, frame)1208+ if self.environment._async:1209+ self.write(')')1210 self.write(', loop)')1211 self.end_write(frame)1212 1213 def visit_If(self, node, frame):524 unmodified linesmodified:tests/test_async.py+11 added
94 unmodified lines95 rv = list(t.generate())96 assert rv == ['1', '2', '3']97 98 99+@pytest.mark.skipif(not have_async_gen, reason='No async generators')100+def test_async_iteration_in_tmeplates():101+ t = Template('{% for x in rng %}{{ x }}{% endfor %}',102+ enable_async=True)103+ async def async_iterator():104+ for item in [1, 2, 3]:105+ yield item106+ rv = list(t.generate(rng=async_iterator()))107+ assert rv == ['1', '2', '3']108+109+110 @pytest.fixture111 def test_env_async():112 env = Environment(loader=DictLoader(dict(113 module='{% macro test() %}[{{ foo }}|{{ bar }}]{% endmacro %}',130 unmodified lines