Skip to content
Myr

Improved wrapper code so that the API is more consistent on failure

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 48baa10 · Browse files · 2 files+36 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

2 files+36 added−6 removed

  • other:+36 added−6 removed

2 files+36 added−6 removed

modified:jinja2/asyncsupport.py+21 added-6 removed
1# -*- coding: utf-8 -*-2"""3 jinja2.asyncsupport4 ~~~~~~~~~~~~~~~~~~~56 Has all the code for async support which is implemented as a patch7 for supported Python versions.89 :copyright: (c) 2017 by the Jinja Team.10 :license: BSD, see LICENSE for more details.11"""12import sys13import asyncio14import inspect15from functools import update_wrapper1617from jinja2.utils import concat, internalcode, concat, Markup18from jinja2.environment import TemplateModule19from jinja2.runtime import LoopContextBase, _last_iteration
31 unmodified lines
51 def generate(self, *args, **kwargs):52 if not self.environment._async:53 return original_generate(self, *args, **kwargs)54 return _convert_generator(self, asyncio.get_event_loop(), args, kwargs)43 return generate55 return update_wrapper(generate, original_generate)565758async def render_async(self, *args, **kwargs):59 if not self.environment._async:
15 unmodified lines
75 if not self.environment._async:76 return original_render(self, *args, **kwargs)77 loop = asyncio.get_event_loop()78 return loop.run_until_complete(self.render_async(*args, **kwargs))67 return render79 return update_wrapper(render, original_render)808182def wrap_block_reference_call(original_call):83 @internalcode
8 unmodified lines
92 if not self._context.environment._async:93 return original_call(self)94 return async_call(self)9584 return __call__96 return update_wrapper(__call__, original_call)979899@internalcode100async def get_default_module_async(self):
22 unmodified lines
123124125def patch_template():126 from jinja2 import Template115 Template.generate_async = generate_async127 Template.generate = wrap_generate_func(Template.generate)117 Template.render_async = render_async128 Template.generate_async = update_wrapper(129 generate_async, Template.generate_async)130 Template.render_async = update_wrapper(131 render_async, Template.render_async)132 Template.render = wrap_render_func(Template.render)133 Template._get_default_module = wrap_default_module(134 Template._get_default_module)135 Template._get_default_module_async = get_default_module_async122 Template.make_module_async = make_module_async136 Template.make_module_async = update_wrapper(137 make_module_async, Template.make_module_async)138139140def patch_runtime():141 from jinja2.runtime import BlockReference
63 unmodified lines
modified:jinja2/environment.py+15 added
989 unmodified lines
990 except Exception:991 exc_info = sys.exc_info()992 return self.environment.handle_exception(exc_info, True)993994 def render_async(self, *args, **kwargs):995 # see asyncsupport for the actual implementation996 raise NotImplementedError('This feature is not available for this '997 'version of Python')998999 def stream(self, *args, **kwargs):1000 """Works exactly like :meth:`generate` but returns a1001 :class:`TemplateStream`.1002 """
16 unmodified lines
1019 else:1020 return1021 yield self.environment.handle_exception(exc_info, True)10221023 def generate_async(self, *args, **kwargs):1024 # see asyncsupport for the actual implementation1025 raise NotImplementedError('This feature is not available for this '1026 'version of Python')10271028 def new_context(self, vars=None, shared=False, locals=None):1029 """Create a new :class:`Context` for this template. The vars1030 provided will be passed to the template. Per default the globals1031 are added to the context. If shared is set to `True` the data
12 unmodified lines
1044 as for the :meth:`new_context` method.1045 """1046 return TemplateModule(self, self.new_context(vars, shared, locals))10471048 def make_module_async(self, vars=None, shared=False, locals=None):1049 # see asyncsupport for the actual implementation1050 raise NotImplementedError('This feature is not available for this '1051 'version of Python')10521053 @internalcode1054 def _get_default_module(self):1055 if self._module is not None:1056 return self._module
186 unmodified lines