Merge branch '2.11.x'
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parents e08dadd, be0e0b0 · Browse files · 4 files·+37 added−28 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+37 added−28 removed
- tests:+7 added−0 removed
- other:+30 added−28 removed
Provenance
- Commit
45a76a3- Tree
eabc964- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent 1
e08dadd- Parent 2
be0e0b0
4 files changed+37 added−28 removed
modified:docs/api.rst+27 added-26 removed
475 unmodified lines476 477 Async Support478 -------------479 480-Starting with version 2.9, Jinja also supports the Python `async` and481-`await` constructs. As far as template designers go this feature is482-entirely opaque to them however as a developer you should be aware of how483-it's implemented as it influences what type of APIs you can safely expose484-to the template environment.480+.. versionadded:: 2.9481 486-First you need to be aware that by default async support is disabled as487-enabling it will generate different template code behind the scenes which488-passes everything through the asyncio event loop. This is important to489-understand because it has some impact to what you are doing:482+Jinja supports the Python ``async`` and ``await`` syntax. For the483+template designer, this support (when enabled) is entirely transparent,484+templates continue to look exactly the same. However, developers should485+be aware of the implementation as it affects what types of APIs you can486+use.487 491-* template rendering will require an event loop to be set for the492- current thread (``asyncio.get_event_loop`` needs to return one)493-* all template generation code internally runs async generators which494- means that you will pay a performance penalty even if the non sync495- methods are used!496-* The sync methods are based on async methods if the async mode is497- enabled which means that `render` for instance will internally invoke498- `render_async` and run it as part of the current event loop until the499- execution finished.488+By default, async support is disabled. Enabling it will cause the489+environment to compile different code behind the scenes in order to490+handle async and sync code in an asyncio event loop. This has the491+following implications:492 493+- Template rendering requires an event loop to be available to the494+ current thread. :func:`asyncio.get_event_loop` must return an event495+ loop.496+- The compiled code uses ``await`` for functions and attributes, and497+ uses ``async for`` loops. In order to support using both async and498+ sync functions in this context, a small wrapper is placed around499+ all calls and access, which add overhead compared to purely async500+ code.501+- Sync methods and filters become wrappers around their corresponding502+ async implementations where needed. For example, ``render`` invokes503+ ``async_render``, and ``|map`` supports async iterables.504+505 Awaitable objects can be returned from functions in templates and any502-function call in a template will automatically await the result. This503-means that you can provide a method that asynchronously loads data504-from a database if you so desire and from the template designer's point of505-view this is just another function they can call. This means that the506-``await`` you would normally issue in Python is implied. However this507-only applies to function calls. If an attribute for instance would be an508-awaitable object then this would not result in the expected behavior.506+function call in a template will automatically await the result. The507+``await`` you would normally add in Python is implied. For example, you508+can provide a method that asynchronously loads data from a database, and509+from the template designer's point of view it can be called like any510+other function.511 510-Likewise iterations with a `for` loop support async iterators.512 513 .. _policies:514 515 Policies355 unmodified linesmodified:src/jinja2/filters.py+1 added-1 removed
739 unmodified lines740 741 742 def do_wordcount(s):743 """Count the words in that string."""744- return len(_word_re.findall(s))744+ return len(_word_re.findall(soft_str(s)))745 746 747 def do_int(value, default=0, base=10):748 """Convert the value into an integer. If the613 unmodified linesmodified:tests/test_filters.py+7 added
3 unmodified lines4 import pytest5 6 from jinja2 import Environment7 from jinja2 import Markup8+from jinja2 import StrictUndefined9+from jinja2 import UndefinedError10 11 12 class Magic:13 def __init__(self, value):350 unmodified lines364 def test_wordcount(self, env):365 tmpl = env.from_string('{{ "foo bar baz"|wordcount }}')366 assert tmpl.render() == "3"367 368+ strict_env = Environment(undefined=StrictUndefined)369+ t = strict_env.from_string("{{ s|wordcount }}")370+ with pytest.raises(UndefinedError):371+ t.render()372+373 def test_block(self, env):374 tmpl = env.from_string("{% filter lower|escape %}<HEHE>{% endfilter %}")375 assert tmpl.render() == "<hehe>"376 369 unmodified linesmodified:CHANGES.rst+2 added-1 removed
17 unmodified lines18 19 - Fix a bug that caused callable objects with ``__getattr__``, like20 :class:`~unittest.mock.Mock` to be treated as a21 :func:`contextfunction`. :issue:`1145`22+- Update ``wordcount`` filter to trigger :class:`Undefined` methods23+ by wrapping the input in :func:`soft_str`. :pr:`1160`24 23-25 Version 2.11.126 --------------27 28 Released 2020-01-30735 unmodified lines