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
Provenance
- Commit
efa5d31- Tree
80b3704- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
48baa10
2 files changed+36 added−6 removed
modified:jinja2/asyncsupport.py+21 added-6 removed
1+# -*- coding: utf-8 -*-2+"""3+ jinja2.asyncsupport4+ ~~~~~~~~~~~~~~~~~~~5+6+ Has all the code for async support which is implemented as a patch7+ for supported Python versions.8+9+ :copyright: (c) 2017 by the Jinja Team.10+ :license: BSD, see LICENSE for more details.11+"""12 import sys13 import asyncio14 import inspect15+from functools import update_wrapper16 17 from jinja2.utils import concat, internalcode, concat, Markup18 from jinja2.environment import TemplateModule19 from jinja2.runtime import LoopContextBase, _last_iteration31 unmodified lines51 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)56 57 58 async def render_async(self, *args, **kwargs):59 if not self.environment._async:15 unmodified lines75 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)80 81 82 def wrap_block_reference_call(original_call):83 @internalcode8 unmodified lines92 if not self._context.environment._async:93 return original_call(self)94 return async_call(self)95 84- return __call__96+ return update_wrapper(__call__, original_call)97 98 99 @internalcode100 async def get_default_module_async(self):22 unmodified lines123 124 125 def 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)138 139 140 def patch_runtime():141 from jinja2.runtime import BlockReference63 unmodified linesmodified:jinja2/environment.py+15 added
989 unmodified lines990 except Exception:991 exc_info = sys.exc_info()992 return self.environment.handle_exception(exc_info, True)993 994+ 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')998+999 def stream(self, *args, **kwargs):1000 """Works exactly like :meth:`generate` but returns a1001 :class:`TemplateStream`.1002 """16 unmodified lines1019 else:1020 return1021 yield self.environment.handle_exception(exc_info, True)1022 1023+ 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')1027+1028 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 data12 unmodified lines1044 as for the :meth:`new_context` method.1045 """1046 return TemplateModule(self, self.new_context(vars, shared, locals))1047 1048+ 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')1052+1053 @internalcode1054 def _get_default_module(self):1055 if self._module is not None:1056 return self._module186 unmodified lines