Skip to content

Commit 74f76fd

Browse files
committed
Checking for circular extends and tokens between blocks
1 parent 6dfcbb1 commit 74f76fd

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

adafruit_templateengine.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ def _resolve_includes(template: str):
299299

300300

301301
def _resolve_includes_blocks_and_extends(template: str):
302+
extended_templates: "set[str]" = set()
302303
block_replacements: "dict[str, str]" = {}
303304

304305
# Processing nested child templates
@@ -308,6 +309,19 @@ def _resolve_includes_blocks_and_extends(template: str):
308309
if not _exists_and_is_file(extended_template_path):
309310
raise OSError(f"Template file not found: {extended_template_path}")
310311

312+
# Check for circular extends
313+
if extended_template_path in extended_templates:
314+
raise TemplateSyntaxError(
315+
f"Circular extends",
316+
Token(
317+
template,
318+
extends_match.start(),
319+
extends_match.end(),
320+
),
321+
)
322+
else:
323+
extended_templates.add(extended_template_path)
324+
311325
# Load extended template
312326
with open(
313327
extended_template_path, "rt", encoding="utf-8"
@@ -334,16 +348,16 @@ def _resolve_includes_blocks_and_extends(template: str):
334348
while (block_match := _find_block(template[offset:])) is not None:
335349
block_name = block_match.group(0)[9:-3]
336350

337-
# Check for any unopened endblock tags before current block
338-
if unopened_endblock_match := _find_endblock(
351+
# Check for any tokens between blocks
352+
if token_between_blocks_match := _find_token(
339353
template[offset : offset + block_match.start()]
340354
):
341355
raise TemplateSyntaxError(
342-
"No matching {% block %}",
356+
"Token between blocks",
343357
Token(
344358
template,
345-
offset + unopened_endblock_match.start(),
346-
offset + unopened_endblock_match.end(),
359+
offset + token_between_blocks_match.start(),
360+
offset + token_between_blocks_match.end(),
347361
),
348362
)
349363

0 commit comments

Comments
 (0)