]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
use asyncio.run 1952/head
authorThomas Grainger <tagrain@gmail.com>
Sun, 7 Apr 2024 08:05:54 +0000 (09:05 +0100)
committerDavid Lord <davidism@gmail.com>
Sat, 11 May 2024 20:41:46 +0000 (13:41 -0700)
CHANGES.rst
docs/api.rst
src/jinja2/environment.py

index 92a5469740874b184045ea0fd4a3a4642b377997..bd085d0304fcdd27fb6a58848bc0a381c7050cd6 100644 (file)
@@ -5,6 +5,9 @@ Version 3.1.5
 
 Unreleased
 
+-   Calling sync ``render`` for an async template uses ``asyncio.run``.
+    :pr:`1952`
+
 
 Version 3.1.4
 -------------
@@ -138,9 +141,8 @@ Released 2021-05-18
     extensions shows more relevant context. :issue:`1429`
 -   Fixed calling deprecated ``jinja2.Markup`` without an argument.
     Use ``markupsafe.Markup`` instead. :issue:`1438`
--   Calling sync ``render`` for an async template uses ``asyncio.run``
-    on Python >= 3.7. This fixes a deprecation that Python 3.10
-    introduces. :issue:`1443`
+-   Calling sync ``render`` for an async template uses ``asyncio.new_event_loop``
+    This fixes a deprecation that Python 3.10 introduces. :issue:`1443`
 
 
 Version 3.0.0
index e2c9bd526b0df6b711392c3fc47f3f5dec081bc9..cb62f6c327a40ce20872e2f1d5164ece8ff13748 100644 (file)
@@ -515,9 +515,6 @@ environment to compile different code behind the scenes in order to
 handle async and sync code in an asyncio event loop. This has the
 following implications:
 
--   Template rendering requires an event loop to be available to the
-    current thread. :func:`asyncio.get_running_loop` must return an
-    event loop.
 -   The compiled code uses ``await`` for functions and attributes, and
     uses ``async for`` loops. In order to support using both async and
     sync functions in this context, a small wrapper is placed around
index 1d3be0bed08d710f2a93079d62552d63fb09b627..ed4198600556f56b7250f3936411b3963e4855ba 100644 (file)
@@ -1282,19 +1282,7 @@ class Template:
         if self.environment.is_async:
             import asyncio
 
-            close = False
-
-            try:
-                loop = asyncio.get_running_loop()
-            except RuntimeError:
-                loop = asyncio.new_event_loop()
-                close = True
-
-            try:
-                return loop.run_until_complete(self.render_async(*args, **kwargs))
-            finally:
-                if close:
-                    loop.close()
+            return asyncio.run(self.render_async(*args, **kwargs))
 
         ctx = self.new_context(dict(*args, **kwargs))