Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into custom_escape
Browse files Browse the repository at this point in the history
# Conflicts:
#	docs/conf.py
#	src/jinja2/asyncsupport.py
#	src/jinja2/environment.py
#	src/jinja2/filters.py
#	src/jinja2/runtime.py
  • Loading branch information
CarliJoy committed Apr 13, 2021
2 parents e057fc0 + f04bde8 commit b6ecd1f
Show file tree
Hide file tree
Showing 24 changed files with 586 additions and 698 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/asottile/pyupgrade
rev: v2.11.0
rev: v2.12.0
hooks:
- id: pyupgrade
args: ["--py36-plus"]
Expand Down
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ Unreleased
- ``pass_environment`` replaces ``environmentfunction`` and
``environmentfilter``.

- Async support no longer requires Jinja to patch itself. It must
still be enabled with ``Environment(enable_async=True)``.
:issue:`1390`
- Overriding ``Context.resolve`` is deprecated, override
``resolve_or_missing`` instead. :issue:`1380`


Version 2.11.3
--------------
Expand Down
2 changes: 1 addition & 1 deletion docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ The Context
-----------

.. autoclass:: jinja2.runtime.Context()
:members: resolve, get_exported, get_all
:members: get, resolve, resolve_or_missing, get_exported, get_all

.. attribute:: parent

Expand Down
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"sphinxcontrib.log_cabinet",
"sphinx_issues",
]
autodoc_typehints = "description"
intersphinx_mapping = {
"python": ("https://docs.python.org/3/", None),
"markupsafe": ("https://markupsafe.palletsprojects.com/", None),
Expand Down
4 changes: 2 additions & 2 deletions docs/intro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ It includes:
- HTML templates can use autoescaping to prevent XSS from untrusted
user input.
- A sandboxed environment can safely render untrusted templates.
- AsyncIO support for generating templates and calling async
functions.
- Async support for generating templates that automatically handle
sync and async functions without extra syntax.
- I18N support with Babel.
- Templates are compiled to optimized Python code just-in-time and
cached, or can be compiled ahead-of-time.
Expand Down
13 changes: 11 additions & 2 deletions docs/templates.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
.. py:currentmodule:: jinja2
.. highlight:: html+jinja

Template Designer Documentation
===============================

.. highlight:: html+jinja

This document describes the syntax and semantics of the template engine and
will be most useful as reference to those creating Jinja templates. As the
template engine is very flexible, the configuration from the application can
Expand Down Expand Up @@ -1485,6 +1486,8 @@ is a bit contrived in the context of rendering a template):
List of Builtin Filters
-----------------------

.. py:currentmodule:: jinja-filters
.. jinja:filters:: jinja2.defaults.DEFAULT_FILTERS
Expand All @@ -1493,6 +1496,8 @@ List of Builtin Filters
List of Builtin Tests
---------------------

.. py:currentmodule:: jinja-tests
.. jinja:tests:: jinja2.defaults.DEFAULT_TESTS
Expand All @@ -1503,6 +1508,8 @@ List of Global Functions

The following functions are available in the global scope by default:

.. py:currentmodule:: jinja-globals
.. function:: range([start,] stop[, step])

Return a list containing an arithmetic progression of integers.
Expand Down Expand Up @@ -1626,6 +1633,8 @@ The following functions are available in the global scope by default:
Extensions
----------

.. py:currentmodule:: jinja2
The following sections cover the built-in Jinja extensions that may be
enabled by an application. An application could also provide further
extensions not covered by this documentation; in which case there should
Expand Down
45 changes: 24 additions & 21 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# This file is autogenerated by pip-compile
# To update, run:
#
# pip-compile requirements/dev.in
# pip-compile ../requirements/dev.in
#
alabaster==0.7.12
# via sphinx
Expand All @@ -28,7 +28,7 @@ filelock==3.0.12
# via
# tox
# virtualenv
identify==1.5.13
identify==2.2.3
# via pre-commit
idna==2.10
# via requests
Expand All @@ -42,9 +42,9 @@ markupsafe==1.1.1
# via jinja2
mypy-extensions==0.4.3
# via mypy
mypy==0.800
# via -r requirements/typing.in
nodeenv==1.5.0
mypy==0.812
# via -r ../requirements/typing.in
nodeenv==1.6.0
# via pre-commit
packaging==20.9
# via
Expand All @@ -53,25 +53,27 @@ packaging==20.9
# sphinx
# tox
pallets-sphinx-themes==1.2.3
# via -r requirements/docs.in
pip-tools==5.5.0
# via -r requirements/dev.in
# via -r ../requirements/docs.in
pep517==0.10.0
# via pip-tools
pip-tools==6.0.1
# via -r ../requirements/dev.in
pluggy==0.13.1
# via
# pytest
# tox
pre-commit==2.10.1
# via -r requirements/dev.in
pre-commit==2.12.0
# via -r ../requirements/dev.in
py==1.10.0
# via
# pytest
# tox
pygments==2.7.4
pygments==2.8.1
# via sphinx
pyparsing==2.4.7
# via packaging
pytest==6.2.2
# via -r requirements/tests.in
pytest==6.2.3
# via -r ../requirements/tests.in
pytz==2021.1
# via babel
pyyaml==5.4.1
Expand All @@ -85,10 +87,10 @@ six==1.15.0
snowballstemmer==2.1.0
# via sphinx
sphinx-issues==1.2.0
# via -r requirements/docs.in
sphinx==2.4.4
# via -r ../requirements/docs.in
sphinx==3.5.4
# via
# -r requirements/docs.in
# -r ../requirements/docs.in
# pallets-sphinx-themes
# sphinx-issues
# sphinxcontrib-log-cabinet
Expand All @@ -101,25 +103,26 @@ sphinxcontrib-htmlhelp==1.0.3
sphinxcontrib-jsmath==1.0.1
# via sphinx
sphinxcontrib-log-cabinet==1.0.1
# via -r requirements/docs.in
# via -r ../requirements/docs.in
sphinxcontrib-qthelp==1.0.3
# via sphinx
sphinxcontrib-serializinghtml==1.1.4
# via sphinx
toml==0.10.2
# via
# pep517
# pre-commit
# pytest
# tox
tox==3.21.4
# via -r requirements/dev.in
typed-ast==1.4.2
tox==3.23.0
# via -r ../requirements/dev.in
typed-ast==1.4.3
# via mypy
typing-extensions==3.7.4.3
# via mypy
urllib3==1.26.4
# via requests
virtualenv==20.4.2
virtualenv==20.4.3
# via
# pre-commit
# tox
Expand Down
2 changes: 1 addition & 1 deletion requirements/docs.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Pallets-Sphinx-Themes
Sphinx<3
Sphinx
sphinx-issues
sphinxcontrib-log-cabinet
14 changes: 7 additions & 7 deletions requirements/docs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# This file is autogenerated by pip-compile
# To update, run:
#
# pip-compile requirements/docs.in
# pip-compile ../requirements/docs.in
#
alabaster==0.7.12
# via sphinx
Expand All @@ -27,8 +27,8 @@ packaging==20.9
# pallets-sphinx-themes
# sphinx
pallets-sphinx-themes==1.2.3
# via -r requirements/docs.in
pygments==2.7.4
# via -r ../requirements/docs.in
pygments==2.8.1
# via sphinx
pyparsing==2.4.7
# via packaging
Expand All @@ -39,10 +39,10 @@ requests==2.25.1
snowballstemmer==2.1.0
# via sphinx
sphinx-issues==1.2.0
# via -r requirements/docs.in
sphinx==2.4.4
# via -r ../requirements/docs.in
sphinx==3.5.4
# via
# -r requirements/docs.in
# -r ../requirements/docs.in
# pallets-sphinx-themes
# sphinx-issues
# sphinxcontrib-log-cabinet
Expand All @@ -55,7 +55,7 @@ sphinxcontrib-htmlhelp==1.0.3
sphinxcontrib-jsmath==1.0.1
# via sphinx
sphinxcontrib-log-cabinet==1.0.1
# via -r requirements/docs.in
# via -r ../requirements/docs.in
sphinxcontrib-qthelp==1.0.3
# via sphinx
sphinxcontrib-serializinghtml==1.1.4
Expand Down
6 changes: 3 additions & 3 deletions requirements/tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# This file is autogenerated by pip-compile
# To update, run:
#
# pip-compile requirements/tests.in
# pip-compile ../requirements/tests.in
#
attrs==20.3.0
# via pytest
Expand All @@ -16,7 +16,7 @@ py==1.10.0
# via pytest
pyparsing==2.4.7
# via packaging
pytest==6.2.2
# via -r requirements/tests.in
pytest==6.2.3
# via -r ../requirements/tests.in
toml==0.10.2
# via pytest
8 changes: 4 additions & 4 deletions requirements/typing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
# This file is autogenerated by pip-compile
# To update, run:
#
# pip-compile requirements/typing.in
# pip-compile ../requirements/typing.in
#
mypy-extensions==0.4.3
# via mypy
mypy==0.800
# via -r requirements/typing.in
typed-ast==1.4.2
mypy==0.812
# via -r ../requirements/typing.in
typed-ast==1.4.3
# via mypy
typing-extensions==3.7.4.3
# via mypy
76 changes: 76 additions & 0 deletions src/jinja2/async_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import inspect
import typing as t
from functools import wraps

from .utils import _PassArg
from .utils import pass_eval_context

if t.TYPE_CHECKING:
V = t.TypeVar("V")


def async_variant(normal_func):
def decorator(async_func):
pass_arg = _PassArg.from_obj(normal_func)
need_eval_context = pass_arg is None

if pass_arg is _PassArg.environment:

def is_async(args):
return args[0].is_async

else:

def is_async(args):
return args[0].environment.is_async

@wraps(normal_func)
def wrapper(*args, **kwargs):
b = is_async(args)

if need_eval_context:
args = args[1:]

if b:
return async_func(*args, **kwargs)

return normal_func(*args, **kwargs)

if need_eval_context:
wrapper = pass_eval_context(wrapper)

wrapper.jinja_async_variant = True
return wrapper

return decorator


async def auto_await(value):
if inspect.isawaitable(value):
return await value

return value


async def auto_aiter(iterable):
if hasattr(iterable, "__aiter__"):
async for item in iterable:
yield item
else:
for item in iterable:
yield item


async def auto_to_list(
value: "t.Union[t.AsyncIterable[V], t.Iterable[V]]",
) -> "t.List[V]":
seq = []

if hasattr(value, "__aiter__"):
async for item in t.cast(t.AsyncIterable, value):
seq.append(item)
else:
for item in t.cast(t.Iterable, value):
seq.append(item)

return seq
Loading

0 comments on commit b6ecd1f

Please sign in to comment.