From: Adrian Moennich Date: Wed, 15 Mar 2017 18:19:04 +0000 (+0100) Subject: Fix custom contexts in fast resolve mode X-Git-Tag: 2.9.6~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a8a9ec2f29d2fb5227a3d37e7f56005795dd98bf;p=thirdparty%2Fjinja.git Fix custom contexts in fast resolve mode closes #675 --- diff --git a/CHANGES b/CHANGES index fb14601e..a3cba109 100644 --- a/CHANGES +++ b/CHANGES @@ -1,6 +1,12 @@ Jinja2 Changelog ================ +Version 2.9.6 +------------- +(bugfix release, release date to be decided) + +- Fixed custom context behavior in fast resolve mode (#675) + Version 2.9.5 ------------- (bugfix release, released on January 28th 2017) diff --git a/jinja2/runtime.py b/jinja2/runtime.py index 24430016..00d5f03c 100644 --- a/jinja2/runtime.py +++ b/jinja2/runtime.py @@ -11,6 +11,8 @@ import sys from itertools import chain +from types import MethodType + from jinja2.nodes import EvalContext, _context_function_types from jinja2.utils import Markup, soft_unicode, escape, missing, concat, \ internalcode, object_type_repr, evalcontextfunction @@ -167,7 +169,7 @@ class Context(with_metaclass(ContextMeta)): # In case we detect the fast resolve mode we can set up an alias # here that bypasses the legacy code logic. if self._fast_resolve_mode: - self.resolve_or_missing = resolve_or_missing + self.resolve_or_missing = MethodType(resolve_or_missing, self) def super(self, name, current): """Render a parent block.""" diff --git a/tests/test_regression.py b/tests/test_regression.py index f2314dee..8bd26788 100644 --- a/tests/test_regression.py +++ b/tests/test_regression.py @@ -506,6 +506,20 @@ class TestBug(object): assert repr(t) == "('foo', [1, 2])" assert str(t) == "('foo', [1, 2])" + def test_custom_context(self, env): + from jinja2.runtime import Context + + class MyContext(Context): + pass + + class MyEnvironment(Environment): + context_class = MyContext + + loader = DictLoader({'base': '{{ foobar }}', + 'test': '{% extends "base" %}'}) + env = MyEnvironment(loader=loader) + assert env.get_template('test').render(foobar='test') == 'test' + def test_legacy_custom_context(self, env): from jinja2.runtime import Context, Undefined, missing