Skip to content

Commit

Permalink
fix f-string syntax error in code generation
Browse files Browse the repository at this point in the history
  • Loading branch information
sisp committed Dec 19, 2023
1 parent d84a174 commit 65e00c4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Unreleased

- Fix compiler error when checking if required blocks in parent templates are
empty. :pr:`1858`
- Fix f-string syntax error in code generation when importing a macro in a
template whose name contains curly braces. :issue: 1792


Version 3.1.2
Expand Down
8 changes: 4 additions & 4 deletions src/jinja2/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1122,12 +1122,12 @@ def visit_FromImport(self, node: nodes.FromImport, frame: Frame) -> None:
self.writeline(f"if {frame.symbols.ref(alias)} is missing:")
self.indent()
message = (
"the template {included_template.__name__!r}"
f" (imported on {self.position(node)})"
f" does not export the requested name {name!r}"
'f"the template {included_template.__name__!r}" + '
f'"(imported on {self.position(node)})" + '
f'"does not export the requested name {name!r}"'
)
self.writeline(
f"{frame.symbols.ref(alias)} = undefined(f{message!r}, name={name!r})"
f"{frame.symbols.ref(alias)} = undefined({message}, name={name!r})"
)
self.outdent()
if frame.toplevel:
Expand Down
12 changes: 12 additions & 0 deletions tests/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,15 @@ def test_import_as_with_context_deterministic(tmp_path):
expect = [f"'bar{i}': " for i in range(10)]
found = re.findall(r"'bar\d': ", content)[:10]
assert found == expect


def test_import_as_with_curly_braces_in_template_name():
env = Environment(
loader=DictLoader(
{
"template_with_{}": "{% import 'macro' as m %}",
"macro": "{% macro m() %}{% endmacro %}",
}
)
)
env.get_template("template_with_{}")

0 comments on commit 65e00c4

Please sign in to comment.