From: Armin Ronacher Date: Wed, 28 Dec 2016 09:42:47 +0000 (+0100) Subject: Add async flags X-Git-Tag: 2.9~82 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e908a584fab5c1edf520ff1274be9f9deaf98f92;p=thirdparty%2Fjinja.git Add async flags --- diff --git a/jinja2/compiler.py b/jinja2/compiler.py index dee97b9b..9c745bd8 100644 --- a/jinja2/compiler.py +++ b/jinja2/compiler.py @@ -47,6 +47,13 @@ try: except SyntaxError: pass +# does this python version support async for in and async generators? +try: + exec('async def _():\n async for _ in ():\n yield _') + have_async_gen = True +except SyntaxError: + have_async_gen = False + # does if 0: dummy(x) get us x into the scope? def unoptimize_before_dead_code(): diff --git a/jinja2/environment.py b/jinja2/environment.py index db51e941..100a0a43 100644 --- a/jinja2/environment.py +++ b/jinja2/environment.py @@ -217,6 +217,11 @@ class Environment(object): have to be parsed if they were not changed. See :ref:`bytecode-cache` for more information. + + `enable_async` + If set to true this enables async template execution which allows + you to take advantage of newer Python features. This requires + Python 3.6 or later. """ #: if this environment is sandboxed. Modifying this variable won't make @@ -268,7 +273,8 @@ class Environment(object): loader=None, cache_size=400, auto_reload=True, - bytecode_cache=None): + bytecode_cache=None, + enable_async=False): # !!Important notice!! # The constructor accepts quite a few arguments that should be # passed by keyword rather than position. However it's important to @@ -314,6 +320,8 @@ class Environment(object): # load extensions self.extensions = load_extensions(self, extensions) + self.enable_async = enable_async + _environment_sanity_check(self) def add_extension(self, extension): @@ -908,14 +916,15 @@ class Template(object): optimized=True, undefined=Undefined, finalize=None, - autoescape=False): + autoescape=False, + enable_async=False): env = get_spontaneous_environment( block_start_string, block_end_string, variable_start_string, variable_end_string, comment_start_string, comment_end_string, line_statement_prefix, line_comment_prefix, trim_blocks, lstrip_blocks, newline_sequence, keep_trailing_newline, frozenset(extensions), optimized, undefined, finalize, autoescape, - None, 0, False, None) + None, 0, False, None, enable_async) return env.from_string(source, template_class=cls) @classmethod