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 547e6e3, 9fe06d8 · Browse files · 7 files+50 added−21 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

7 files+50 added−21 removed

  • tests:+19 added−0 removed
  • other:+31 added−21 removed

7 files+50 added−21 removed

modified:src/jinja2/asyncfilters.py+4 added-5 removed
24 unmodified lines
252627def dualfilter(normal_filter, async_filter):28 wrap_evalctx = False29 if getattr(normal_filter, "environmentfilter", False):29 if getattr(normal_filter, "environmentfilter", False) is True:3031 def is_async(args):32 return args[0].is_async3334 wrap_evalctx = False35 else:36 if not getattr(normal_filter, "evalcontextfilter", False) and not getattr(37 normal_filter, "contextfilter", False38 ):39 wrap_evalctx = True36 has_evalctxfilter = getattr(normal_filter, "evalcontextfilter", False) is True37 has_ctxfilter = getattr(normal_filter, "contextfilter", False) is True38 wrap_evalctx = not has_evalctxfilter and not has_ctxfilter3940 def is_async(args):41 return args[0].environment.is_async42
116 unmodified lines
modified:src/jinja2/compiler.py+6 added-6 removed
1305 unmodified lines
13061307 def finalize(value):1308 return default(env_finalize(value))13091310 if getattr(env_finalize, "contextfunction", False):1310 if getattr(env_finalize, "contextfunction", False) is True:1311 src += "context, "1312 finalize = None # noqa: F8111313 elif getattr(env_finalize, "evalcontextfunction", False):1313 elif getattr(env_finalize, "evalcontextfunction", False) is True:1314 src += "context.eval_ctx, "1315 finalize = None1316 elif getattr(env_finalize, "environmentfunction", False):1316 elif getattr(env_finalize, "environmentfunction", False) is True:1317 src += "environment, "13181319 def finalize(value):1320 return default(env_finalize(self.environment, value))
367 unmodified lines
1688 self.write(self.filters[node.name] + "(")1689 func = self.environment.filters.get(node.name)1690 if func is None:1691 self.fail("no filter named %r" % node.name, node.lineno)1692 if getattr(func, "contextfilter", False):1692 if getattr(func, "contextfilter", False) is True:1693 self.write("context, ")1694 elif getattr(func, "evalcontextfilter", False):1694 elif getattr(func, "evalcontextfilter", False) is True:1695 self.write("context.eval_ctx, ")1696 elif getattr(func, "environmentfilter", False):1696 elif getattr(func, "environmentfilter", False) is True:1697 self.write("environment, ")16981699 # if the filter node is None we are inside a filter block1700 # and want to write to the current buffer
143 unmodified lines
modified:src/jinja2/environment.py+3 added-3 removed
490 unmodified lines
491 func = self.filters.get(name)492 if func is None:493 fail_for_missing_callable("no filter named %r", name)494 args = [value] + list(args or ())495 if getattr(func, "contextfilter", False):495 if getattr(func, "contextfilter", False) is True:496 if context is None:497 raise TemplateRuntimeError(498 "Attempted to invoke context filter without context"499 )500 args.insert(0, context)501 elif getattr(func, "evalcontextfilter", False):501 elif getattr(func, "evalcontextfilter", False) is True:502 if eval_ctx is None:503 if context is not None:504 eval_ctx = context.eval_ctx505 else:506 eval_ctx = EvalContext(self)507 args.insert(0, eval_ctx)508 elif getattr(func, "environmentfilter", False):508 elif getattr(func, "environmentfilter", False) is True:509 args.insert(0, self)510 return func(*args, **(kwargs or {}))511512 def call_test(self, name, value, args=None, kwargs=None):
850 unmodified lines
modified:src/jinja2/nodes.py+3 added-3 removed
669 unmodified lines
670 # builtin filter function here which no longer returns a list in671 # python 3. because of that, do not rename filter_ to filter!672 filter_ = self.environment.filters.get(self.name)673674 if filter_ is None or getattr(filter_, "contextfilter", False):674 if filter_ is None or getattr(filter_, "contextfilter", False) is True:675 raise Impossible()676677 # We cannot constant handle async filters, so we need to make sure678 # to not go down this path.
4 unmodified lines
683684 args, kwargs = args_as_const(self, eval_ctx)685 args.insert(0, self.node.as_const(eval_ctx))686687 if getattr(filter_, "evalcontextfilter", False):687 if getattr(filter_, "evalcontextfilter", False) is True:688 args.insert(0, eval_ctx)689 elif getattr(filter_, "environmentfilter", False):689 elif getattr(filter_, "environmentfilter", False) is True:690 args.insert(0, self.environment)691692 try:693 return filter_(*args, **kwargs)
395 unmodified lines
modified:src/jinja2/runtime.py+3 added-3 removed
278 unmodified lines
279 __obj = fn280 break281282 if callable(__obj):283 if getattr(__obj, "contextfunction", 0):283 if getattr(__obj, "contextfunction", False) is True:284 args = (__self,) + args285 elif getattr(__obj, "evalcontextfunction", 0):285 elif getattr(__obj, "evalcontextfunction", False) is True:286 args = (__self.eval_ctx,) + args287 elif getattr(__obj, "environmentfunction", 0):287 elif getattr(__obj, "environmentfunction", False) is True:288 args = (__self.environment,) + args289 try:290 return __obj(*args, **kwargs)291 except StopIteration:
720 unmodified lines
modified:tests/test_runtime.py+19 added
52 unmodified lines
53 )54 # groupby groups depend on the current position of the iterator. If55 # it was advanced early, the lists would appear empty.56 assert out == "1 [(1, 'a'), (1, 'b')]\n2 [(2, 'c')]\n3 [(3, 'd')]\n"575859def test_mock_not_contextfunction():60 """If a callable class has a ``__getattr__`` that returns True-like61 values for arbitrary attrs, it should not be incorrectly identified62 as a ``contextfunction``.63 """6465 class Calc(object):66 def __getattr__(self, item):67 return object()6869 def __call__(self, *args, **kwargs):70 return len(args) + len(kwargs)7172 t = Template("{{ calc() }}")73 out = t.render(calc=Calc())74 # Would be "1" if context argument was passed.75 assert out == "0"
modified:CHANGES.rst+12 added-1 removed
8 unmodified lines
9- Bump MarkupSafe dependency to >=1.1.10- Bump Babel optional dependency to >=2.1.1112132.11.214------1516Unreleased1718- Fix a bug that caused callable objects with ``__getattr__``, like19 :class:`~unittest.mock.Mock` to be treated as a20 :func:`contextfunction`. :issue:`1145`21222324Version 2.11.125--------------2616Unreleased27Released 2020-01-302829- Fix a bug that prevented looking up a key after an attribute30 (``{{ data.items[1:] }}``) in an async template. :issue:`1141`31
731 unmodified lines