]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Add async flags
authorArmin Ronacher <armin.ronacher@active-4.com>
Wed, 28 Dec 2016 09:42:47 +0000 (10:42 +0100)
committerArmin Ronacher <armin.ronacher@active-4.com>
Wed, 28 Dec 2016 09:42:47 +0000 (10:42 +0100)
jinja2/compiler.py
jinja2/environment.py

index dee97b9b167c520f6b567139f16c59c7afc998c1..9c745bd8148731a0858e0d430ec472e80aa1e1b1 100644 (file)
@@ -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():
index db51e941b511e954f73c1d321c1b3bda7919386c..100a0a437f8f474ea2cd1c22757a8c727a3f501b 100644 (file)
@@ -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