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 authored and davidism committed Dec 19, 2024
1 parent 48b0687 commit 2a6be86
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Unreleased
- The sandboxed environment handles indirect calls to ``str.format``, such as
by passing a stored reference to a filter that calls its argument.
:ghsa:`q2x7-8rv6-6q7h`
- Escape template name before formatting it into error messages, to avoid
issues with names that contain f-string syntax.
:issue:`1792`, :ghsa:`gmj6-6f8f-6699`
- Sandbox does not allow ``clear`` and ``pop`` on known mutable sequence
types. :issue:`2032`
- Calling sync ``render`` for an async template uses ``asyncio.run``.
Expand Down
8 changes: 4 additions & 4 deletions src/jinja2/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1142,12 +1142,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 @@ -87,3 +87,15 @@ def test_block_set_vars_unpacking_deterministic(tmp_path):
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 2a6be86

Please sign in to comment.