@@ -79,9 +79,9 @@ def __init__(self, path: str):
7979class TemplateSyntaxError (SyntaxError ):
8080 """Raised when a syntax error is encountered in a template."""
8181
82- def __init__ (self , message : str , token : Token ):
82+ def __init__ (self , token : Token , reason : str ):
8383 """Provided token is not a valid template syntax at the specified position."""
84- super ().__init__ (f" { message } \n \n " + self . _underline_token_in_template ( token ) )
84+ super ().__init__ (self . _underline_token_in_template ( token ) + f" \n \n { reason } " )
8585
8686 @staticmethod
8787 def _skipped_lines_message (nr_of_lines : int ) -> str :
@@ -92,8 +92,8 @@ def _underline_token_in_template(
9292 cls , token : Token , * , lines_around : int = 4 , symbol : str = "^"
9393 ) -> str :
9494 """
95- Return ``number_of_lines`` lines before and after the token, with the token content underlined
96- with ``symbol`` e.g.:
95+ Return ``number_of_lines`` lines before and after `` token`` , with the token content
96+ underlined with ``symbol`` e.g.:
9797
9898 ```html
9999 [8 lines skipped]
@@ -337,17 +337,16 @@ def _resolve_includes_blocks_and_extends(template: str):
337337 # Check for circular extends
338338 if extended_template_path in extended_templates :
339339 raise TemplateSyntaxError (
340- f"Circular extends" ,
341340 Token (
342341 template ,
343342 extends_match .start (),
344343 extends_match .end (),
345344 ),
345+ "Circular extends" ,
346346 )
347- else :
348- extended_templates .add (extended_template_path )
349347
350348 # Load extended template
349+ extended_templates .add (extended_template_path )
351350 with open (
352351 extended_template_path , "rt" , encoding = "utf-8"
353352 ) as extended_template_file :
@@ -361,12 +360,12 @@ def _resolve_includes_blocks_and_extends(template: str):
361360 # Check for any stacked extends
362361 if stacked_extends_match := _find_extends (template [extends_match .end () :]):
363362 raise TemplateSyntaxError (
364- "Incorrect use of {% extends ... %}" ,
365363 Token (
366364 template ,
367365 extends_match .end () + stacked_extends_match .start (),
368366 extends_match .end () + stacked_extends_match .end (),
369367 ),
368+ "Incorrect use of {% extends ... %}" ,
370369 )
371370
372371 # Save block replacements
@@ -378,22 +377,22 @@ def _resolve_includes_blocks_and_extends(template: str):
378377 template [offset : offset + block_match .start ()]
379378 ):
380379 raise TemplateSyntaxError (
381- "Token between blocks" ,
382380 Token (
383381 template ,
384382 offset + token_between_blocks_match .start (),
385383 offset + token_between_blocks_match .end (),
386384 ),
385+ "Token between blocks" ,
387386 )
388387
389388 if not (endblock_match := _find_endblock (template [offset :], block_name )):
390389 raise TemplateSyntaxError (
391- "No matching {% endblock %}" ,
392390 Token (
393391 template ,
394392 offset + block_match .start (),
395393 offset + block_match .end (),
396394 ),
395+ "No matching {% endblock %}" ,
397396 )
398397
399398 block_content = template [
@@ -403,12 +402,12 @@ def _resolve_includes_blocks_and_extends(template: str):
403402 # Check for unsupported nested blocks
404403 if (nested_block_match := _find_block (block_content )) is not None :
405404 raise TemplateSyntaxError (
406- "Nested blocks are not supported" ,
407405 Token (
408406 template ,
409407 offset + block_match .end () + nested_block_match .start (),
410408 offset + block_match .end () + nested_block_match .end (),
411409 ),
410+ "Nested blocks are not supported" ,
412411 )
413412
414413 if block_name in block_replacements :
@@ -450,12 +449,12 @@ def _replace_blocks_with_replacements(template: str, replacements: "dict[str, st
450449 # Check for unsupported nested blocks
451450 if (nested_block_match := _find_block (block_content )) is not None :
452451 raise TemplateSyntaxError (
453- "Nested blocks are not supported" ,
454452 Token (
455453 template ,
456454 block_match .end () + nested_block_match .start (),
457455 block_match .end () + nested_block_match .end (),
458456 ),
457+ "Nested blocks are not supported" ,
459458 )
460459
461460 # No replacement for this block, use default content
@@ -602,7 +601,7 @@ def _create_template_rendering_function( # pylint: disable=,too-many-locals,too
602601 nested_if_statements .append (token )
603602 elif token .content .startswith (r"{% elif " ):
604603 if not nested_if_statements :
605- raise TemplateSyntaxError ("No matching {% if ... %}" , token )
604+ raise TemplateSyntaxError (token , "No matching {% if ... %}" )
606605
607606 indentation_level -= 1
608607 function_string += (
@@ -611,14 +610,14 @@ def _create_template_rendering_function( # pylint: disable=,too-many-locals,too
611610 indentation_level += 1
612611 elif token .content == r"{% else %}" :
613612 if not nested_if_statements :
614- raise TemplateSyntaxError ("No matching {% if ... %}" , token )
613+ raise TemplateSyntaxError (token , "No matching {% if ... %}" )
615614
616615 indentation_level -= 1
617616 function_string += indent * indentation_level + "else:\n "
618617 indentation_level += 1
619618 elif token .content == r"{% endif %}" :
620619 if not nested_if_statements :
621- raise TemplateSyntaxError ("No matching {% if ... %}" , token )
620+ raise TemplateSyntaxError (token , "No matching {% if ... %}" )
622621
623622 indentation_level -= 1
624623 nested_if_statements .pop ()
@@ -633,7 +632,7 @@ def _create_template_rendering_function( # pylint: disable=,too-many-locals,too
633632 nested_for_loops .append (token )
634633 elif token .content == r"{% empty %}" :
635634 if not nested_for_loops :
636- raise TemplateSyntaxError ("No matching {% for ... %}" , token )
635+ raise TemplateSyntaxError (token , "No matching {% for ... %}" )
637636
638637 indentation_level -= 1
639638 last_forloop_iterable = (
@@ -645,7 +644,7 @@ def _create_template_rendering_function( # pylint: disable=,too-many-locals,too
645644 indentation_level += 1
646645 elif token .content == r"{% endfor %}" :
647646 if not nested_for_loops :
648- raise TemplateSyntaxError ("No matching {% for ... %}" , token )
647+ raise TemplateSyntaxError (token , "No matching {% for ... %}" )
649648
650649 indentation_level -= 1
651650 nested_for_loops .pop ()
@@ -660,7 +659,7 @@ def _create_template_rendering_function( # pylint: disable=,too-many-locals,too
660659 nested_while_loops .append (token )
661660 elif token .content == r"{% endwhile %}" :
662661 if not nested_while_loops :
663- raise TemplateSyntaxError ("No matching {% while ... %}" , token )
662+ raise TemplateSyntaxError (token , "No matching {% while ... %}" )
664663
665664 indentation_level -= 1
666665 nested_while_loops .pop ()
@@ -680,39 +679,39 @@ def _create_template_rendering_function( # pylint: disable=,too-many-locals,too
680679
681680 elif token .content == r"{% endautoescape %}" :
682681 if not nested_autoescape_modes :
683- raise TemplateSyntaxError ("No matching {% autoescape ... %}" , token )
682+ raise TemplateSyntaxError (token , "No matching {% autoescape ... %}" )
684683
685684 nested_autoescape_modes .pop ()
686685
687686 # Token is a endblock in top-level template
688687 elif token .content .startswith (r"{% endblock " ):
689- raise TemplateSyntaxError ("No matching {% block ... %}" , token )
688+ raise TemplateSyntaxError (token , "No matching {% block ... %}" )
690689
691690 # Token is a extends in top-level template
692691 elif token .content .startswith (r"{% extends " ):
693- raise TemplateSyntaxError ("Incorrect use of {% extends ... %}" , token )
692+ raise TemplateSyntaxError (token , "Incorrect use of {% extends ... %}" )
694693
695694 else :
696- raise TemplateSyntaxError (f"Unknown token: { token .content } " , token )
695+ raise TemplateSyntaxError (token , f"Unknown token: { token .content } " )
697696
698697 else :
699- raise TemplateSyntaxError (f"Unknown token: { token .content } " , token )
698+ raise TemplateSyntaxError (token , f"Unknown token: { token .content } " )
700699
701700 # Move offset to the end of the token
702701 offset += token_match .end ()
703702
704703 # Checking for unclosed blocks
705704 if len (nested_if_statements ) > 0 :
706705 last_if_statement = nested_if_statements [- 1 ]
707- raise TemplateSyntaxError ("No matching {% endif %}" , last_if_statement )
706+ raise TemplateSyntaxError (last_if_statement , "No matching {% endif %}" )
708707
709708 if len (nested_for_loops ) > 0 :
710709 last_for_loop = nested_for_loops [- 1 ]
711- raise TemplateSyntaxError ("No matching {% endfor %}" , last_for_loop )
710+ raise TemplateSyntaxError (last_for_loop , "No matching {% endfor %}" )
712711
713712 if len (nested_while_loops ) > 0 :
714713 last_while_loop = nested_while_loops [- 1 ]
715- raise TemplateSyntaxError ("No matching {% endwhile %}" , last_while_loop )
714+ raise TemplateSyntaxError (last_while_loop , "No matching {% endwhile %}" )
716715
717716 # No check for unclosed autoescape blocks, as they are optional and do not result in errors
718717
0 commit comments