]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
async_variant filters are pickleable 1613/head
authorJeff Dairiki <dairiki@dairiki.org>
Thu, 3 Mar 2022 20:41:35 +0000 (12:41 -0800)
committerDavid Lord <davidism@gmail.com>
Mon, 7 Mar 2022 15:52:04 +0000 (07:52 -0800)
CHANGES.rst
src/jinja2/async_utils.py
tests/test_pickle.py [new file with mode: 0644]

index 3a779d7b4d3b4976d30df4a3bce96ea4c190dd6c..3e8b56e475440ddfd7ac861a69ce63bba1fb049d 100644 (file)
@@ -28,6 +28,8 @@ Unreleased
     ``{% trans "title" %}``. :issue:`1430`
 -   Update valid identifier characters from Python 3.6 to 3.7.
     :pr:`1571`
+-   Filters and tests decorated with ``@async_variant`` are pickleable.
+    :pr:`1612`
 
 
 Version 3.0.3
index 35e6cb109029b0c07a9a3cd052a45daa2db8dd64..1a4f3892cef1a53632476933f2ce2d86fc31b10a 100644 (file)
@@ -1,5 +1,6 @@
 import inspect
 import typing as t
+from functools import WRAPPER_ASSIGNMENTS
 from functools import wraps
 
 from .utils import _PassArg
@@ -23,7 +24,15 @@ def async_variant(normal_func):  # type: ignore
             def is_async(args: t.Any) -> bool:
                 return t.cast(bool, args[0].environment.is_async)
 
-        @wraps(normal_func)
+        # Take the doc and annotations from the sync function, but the
+        # name from the async function. Pallets-Sphinx-Themes
+        # build_function_directive expects __wrapped__ to point to the
+        # sync function.
+        async_func_attrs = ("__module__", "__name__", "__qualname__")
+        normal_func_attrs = tuple(set(WRAPPER_ASSIGNMENTS).difference(async_func_attrs))
+
+        @wraps(normal_func, assigned=normal_func_attrs)
+        @wraps(async_func, assigned=async_func_attrs, updated=())
         def wrapper(*args, **kwargs):  # type: ignore
             b = is_async(args)
 
diff --git a/tests/test_pickle.py b/tests/test_pickle.py
new file mode 100644 (file)
index 0000000..b0f6bcf
--- /dev/null
@@ -0,0 +1,6 @@
+import pickle
+
+
+def test_environment(env):
+    env = pickle.loads(pickle.dumps(env))
+    assert env.from_string("x={{ x }}").render(x=42) == "x=42"