From: Jeff Dairiki Date: Thu, 3 Mar 2022 20:41:35 +0000 (-0800) Subject: async_variant filters are pickleable X-Git-Tag: 3.1.0~10^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F1613%2Fhead;p=thirdparty%2Fjinja.git async_variant filters are pickleable --- diff --git a/CHANGES.rst b/CHANGES.rst index 3a779d7b..3e8b56e4 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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 diff --git a/src/jinja2/async_utils.py b/src/jinja2/async_utils.py index 35e6cb10..1a4f3892 100644 --- a/src/jinja2/async_utils.py +++ b/src/jinja2/async_utils.py @@ -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 index 00000000..b0f6bcfe --- /dev/null +++ b/tests/test_pickle.py @@ -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"