Skip to content
Myr

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

4 files+37 added−28 removed

modified:docs/api.rst+27 added-26 removed
475 unmodified lines
476477Async Support478-------------479480Starting with version 2.9, Jinja also supports the Python `async` and481`await` constructs. As far as template designers go this feature is482entirely opaque to them however as a developer you should be aware of how483it's implemented as it influences what type of APIs you can safely expose484to the template environment.480.. versionadded:: 2.9481486First you need to be aware that by default async support is disabled as487enabling it will generate different template code behind the scenes which488passes everything through the asyncio event loop. This is important to489understand because it has some impact to what you are doing:482Jinja supports the Python ``async`` and ``await`` syntax. For the483template designer, this support (when enabled) is entirely transparent,484templates continue to look exactly the same. However, developers should485be aware of the implementation as it affects what types of APIs you can486use.487491* 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.488By default, async support is disabled. Enabling it will cause the489environment to compile different code behind the scenes in order to490handle async and sync code in an asyncio event loop. This has the491following implications:492493- 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.504505Awaitable objects can be returned from functions in templates and any502function call in a template will automatically await the result. This503means that you can provide a method that asynchronously loads data504from a database if you so desire and from the template designer's point of505view this is just another function they can call. This means that the506``await`` you would normally issue in Python is implied. However this507only applies to function calls. If an attribute for instance would be an508awaitable object then this would not result in the expected behavior.506function call in a template will automatically await the result. The507``await`` you would normally add in Python is implied. For example, you508can provide a method that asynchronously loads data from a database, and509from the template designer's point of view it can be called like any510other function.511510Likewise iterations with a `for` loop support async iterators.512513.. _policies:514515Policies
355 unmodified lines
modified:src/jinja2/filters.py+1 added-1 removed
739 unmodified lines
740741742def 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)))745746747def do_int(value, default=0, base=10):748 """Convert the value into an integer. If the
613 unmodified lines
modified:tests/test_filters.py+7 added
3 unmodified lines
4import pytest56from jinja2 import Environment7from jinja2 import Markup8from jinja2 import StrictUndefined9from jinja2 import UndefinedError101112class Magic:13 def __init__(self, value):
350 unmodified lines
364 def test_wordcount(self, env):365 tmpl = env.from_string('{{ "foo bar baz"|wordcount }}')366 assert tmpl.render() == "3"367368 strict_env = Environment(undefined=StrictUndefined)369 t = strict_env.from_string("{{ s|wordcount }}")370 with pytest.raises(UndefinedError):371 t.render()372373 def test_block(self, env):374 tmpl = env.from_string("{% filter lower|escape %}<HEHE>{% endfilter %}")375 assert tmpl.render() == "&lt;hehe&gt;"376
369 unmodified lines
modified:CHANGES.rst+2 added-1 removed
17 unmodified lines
1819- 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`242325Version 2.11.126--------------2728Released 2020-01-30
735 unmodified lines