async_variant filters are pickleable

This commit is contained in:
Jeff Dairiki 2022-03-03 12:41:35 -08:00 committed by David Lord
parent 09907ef378
commit ac3ac6c965
No known key found for this signature in database
GPG Key ID: 7A1C87E3F5BC42A8
3 changed files with 18 additions and 1 deletions

View 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

View 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)

6
tests/test_pickle.py Normal file
View File

@ -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"