Merge branch '3.0.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 e20a52e, b7d12b6 · Browse files · 6 files·+58 added−35 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+58 added−35 removed
- tests:+47 added−31 removed
- other:+11 added−4 removed
Provenance
- Commit
f20a9c9- Tree
2ad5155- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent 1
e20a52e- Parent 2
b7d12b6
6 files changed+58 added−35 removed
modified:src/jinja2/compiler.py+1 added-3 removed
1088 unmodified lines1089 f_name = f"make_module{self.choose_async('_async')}"1090 self.write(1091 f"{f_name}(context.get_all(), True, {self.dump_local_context(frame)})"1092 )1093- elif self.environment.is_async:1094- self.write("_get_default_module_async()")1093 else:1096- self.write("_get_default_module(context)")1094+ self.write(f"_get_default_module{self.choose_async('_async')}(context)")1095 1096 def visit_Import(self, node: nodes.Import, frame: Frame) -> None:1097 """Visit regular imports."""1098 self.writeline(f"{frame.symbols.ref(node.target)} = ", node)859 unmodified linesmodified:src/jinja2/loaders.py+3 added-1 removed
268 unmodified lines269 package_name: str,270 package_path: "str" = "templates",271 encoding: str = "utf-8",272 ) -> None:273+ package_path = os.path.normpath(package_path).rstrip(os.path.sep)274+275+ # normpath preserves ".", which isn't valid in zip paths.276 if package_path == os.path.curdir:277 package_path = ""278 elif package_path[:2] == os.path.curdir + os.path.sep:279 package_path = package_path[2:]280 278- package_path = os.path.normpath(package_path).rstrip(os.path.sep)281 self.package_path = package_path282 self.package_name = package_name283 self.encoding = encoding284 360 unmodified linesmodified:tests/test_async.py+23 added
187 unmodified lines188 assert not hasattr(m, "__missing")189 assert m.variable == 42190 assert not hasattr(m, "notthere")191 192+ def test_import_with_globals(self, test_env_async):193+ t = test_env_async.from_string(194+ '{% import "module" as m %}{{ m.test() }}', globals={"foo": 42}195+ )196+ assert t.render() == "[42|23]"197 198+ t = test_env_async.from_string('{% import "module" as m %}{{ m.test() }}')199+ assert t.render() == "[|23]"200+201+ def test_import_with_globals_override(self, test_env_async):202+ t = test_env_async.from_string(203+ '{% set foo = 41 %}{% import "module" as m %}{{ m.test() }}',204+ globals={"foo": 42},205+ )206+ assert t.render() == "[42|23]"207+208+ def test_from_import_with_globals(self, test_env_async):209+ t = test_env_async.from_string(210+ '{% from "module" import test %}{{ test() }}',211+ globals={"foo": 42},212+ )213+ assert t.render() == "[42|23]"214+215+216 class TestAsyncIncludes:217 def test_context_include(self, test_env_async):218 t = test_env_async.from_string('{% include "header" %}')219 assert t.render(foo=42) == "[42|23]"441 unmodified linesmodified:tests/test_imports.py+13 added-31 removed
97 unmodified lines98 with pytest.raises(UndefinedError, match="does not export the requested name"):99 t.render()100 101 def test_import_with_globals(self, test_env):102- env = Environment(103- loader=DictLoader(104- {105- "macros": "{% macro test() %}foo: {{ foo }}{% endmacro %}",106- "test": "{% import 'macros' as m %}{{ m.test() }}",107- "test1": "{% import 'macros' as m %}{{ m.test() }}",108- }109- )102+ t = test_env.from_string(103+ '{% import "module" as m %}{{ m.test() }}', globals={"foo": 42}104 )111- tmpl = env.get_template("test", globals={"foo": "bar"})112- assert tmpl.render() == "foo: bar"105+ assert t.render() == "[42|23]"106 114- tmpl = env.get_template("test1")115- assert tmpl.render() == "foo: "107+ t = test_env.from_string('{% import "module" as m %}{{ m.test() }}')108+ assert t.render() == "[|23]"109 110 def test_import_with_globals_override(self, test_env):118- env = Environment(119- loader=DictLoader(120- {121- "macros": "{% set foo = '42' %}{% macro test() %}"122- "foo: {{ foo }}{% endmacro %}",123- "test": "{% from 'macros' import test %}{{ test() }}",124- }125- )111+ t = test_env.from_string(112+ '{% set foo = 41 %}{% import "module" as m %}{{ m.test() }}',113+ globals={"foo": 42},114 )127- tmpl = env.get_template("test", globals={"foo": "bar"})128- assert tmpl.render() == "foo: 42"115+ assert t.render() == "[42|23]"116 117 def test_from_import_with_globals(self, test_env):131- env = Environment(132- loader=DictLoader(133- {134- "macros": "{% macro testing() %}foo: {{ foo }}{% endmacro %}",135- "test": "{% from 'macros' import testing %}{{ testing() }}",136- }137- )118+ t = test_env.from_string(119+ '{% from "module" import test %}{{ test() }}',120+ globals={"foo": 42},121 )139- tmpl = env.get_template("test", globals={"foo": "bar"})140- assert tmpl.render() == "foo: bar"122+ assert t.render() == "[42|23]"123 124 125 class TestIncludes:126 def test_context_include(self, test_env):79 unmodified linesmodified:tests/test_loader.py+11 added
337 unmodified lines338 def test_package_zip_list(package_zip_loader):339 assert package_zip_loader.list_templates() == ["foo/test.html", "test.html"]340 341 342+@pytest.mark.parametrize("package_path", ["", ".", "./"])343+def test_package_zip_omit_curdir(package_zip_loader, package_path):344+ """PackageLoader should not add or include "." or "./" in the root345+ path, it is invalid in zip paths.346+ """347+ loader = PackageLoader("t_pack", package_path)348+ assert loader.package_path == ""349+ source, _, _ = loader.get_source(None, "templates/foo/test.html")350+ assert source.rstrip() == "FOO"351+352+353 def test_pep_451_import_hook():354 class ImportHook(importlib.abc.MetaPathFinder, importlib.abc.Loader):355 def find_spec(self, name, path=None, target=None):356 if name != "res":25 unmodified linesmodified:CHANGES.rst+7 added
7 unmodified lines8 9 Version 3.0.210 -------------11 12+Released 2021-10-0413+14 - Fix a loop scoping bug that caused assignments in nested loops15 to still be referenced outside of it. :issue:`1427`16 - Make ``compile_templates`` deterministic for filter and import17 names. :issue:`1452, 1453`18 - Revert an unintended change that caused ``Undefined`` to act like19 ``StrictUndefined`` for the ``in`` operator. :issue:`1448`20+- Imported macros have access to the current template globals in async21+ environments. :issue:`1494`22+- ``PackageLoader`` will not include a current directory (.) path23+ segment. This allows loading templates from the root of a zip24+ import. :issue:`1467`25 26 27 Version 3.0.128 -------------876 unmodified lines