From 04294999c766c8524ba087c49b63dc8cfbdb72ca Mon Sep 17 00:00:00 2001 From: David Lord Date: Tue, 4 Feb 2020 08:46:53 -0800 Subject: [PATCH] add changelog and test --- CHANGES.rst | 13 ++++++++++++- tests/test_runtime.py | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index 9ec8a3a4..651ab430 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,9 +1,20 @@ .. currentmodule:: jinja2 +2.11.2 +------ + +Unreleased + +- Fix a bug that caused callable objects with ``__getattr__``, like + :class:`~unittest.mock.Mock` to be treated as a + :func:`contextfunction`. :issue:`1145` + + + Version 2.11.1 -------------- -Unreleased +Released 2020-01-30 - Fix a bug that prevented looking up a key after an attribute (``{{ data.items[1:] }}``) in an async template. :issue:`1141` diff --git a/tests/test_runtime.py b/tests/test_runtime.py index d24f2660..5e4686c0 100644 --- a/tests/test_runtime.py +++ b/tests/test_runtime.py @@ -54,3 +54,22 @@ def test_iterator_not_advanced_early(): # groupby groups depend on the current position of the iterator. If # it was advanced early, the lists would appear empty. assert out == "1 [(1, 'a'), (1, 'b')]\n2 [(2, 'c')]\n3 [(3, 'd')]\n" + + +def test_mock_not_contextfunction(): + """If a callable class has a ``__getattr__`` that returns True-like + values for arbitrary attrs, it should not be incorrectly identified + as a ``contextfunction``. + """ + + class Calc(object): + def __getattr__(self, item): + return object() + + def __call__(self, *args, **kwargs): + return len(args) + len(kwargs) + + t = Template("{{ calc() }}") + out = t.render(calc=Calc()) + # Would be "1" if context argument was passed. + assert out == "0" -- 2.47.2