Skip to content

1.3.4 breaks includes with two variables #401

@vain

Description

@vain

Consider this example:

#!/usr/bin/env python3

from mako.template import Template
from mako.lookup import TemplateLookup

mylookup = TemplateLookup(directories=['.'])
mytemplate = Template('<%include file="${foo}${bar}"/>', lookup=mylookup)
print(mytemplate.render(foo='foo', bar='bar'))

The content of the foobar file does not matter:

$ cat foobar 
This is the included template.

With 1.3.4, I get the following exception:

(env)$ ./test.py 
Traceback (most recent call last):
  File "/tmp/tmp/env/lib/python3.12/site-packages/mako/pyparser.py", line 36, in parse
    return _ast_util.parse(code, "<unknown>", mode)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/tmp/env/lib/python3.12/site-packages/mako/_ast_util.py", line 91, in parse
    return compile(expr, filename, mode, PyCF_ONLY_AST)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<unknown>", line 1
    foo}${bar
       ^
SyntaxError: unmatched '}'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/tmp/tmp/./test.py", line 9, in <module>
    mytemplate = Template('<%include file="${foo}${bar}"/>', lookup=mylookup)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/tmp/env/lib/python3.12/site-packages/mako/template.py", line 299, in __init__
    (code, module) = _compile_text(self, text, filename)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/tmp/env/lib/python3.12/site-packages/mako/template.py", line 676, in _compile_text
    source, lexer = _compile(
                    ^^^^^^^^^
  File "/tmp/tmp/env/lib/python3.12/site-packages/mako/template.py", line 656, in _compile
    node = lexer.parse()
           ^^^^^^^^^^^^^
  File "/tmp/tmp/env/lib/python3.12/site-packages/mako/lexer.py", line 244, in parse
    if self.match_tag_start():
       ^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/tmp/env/lib/python3.12/site-packages/mako/lexer.py", line 312, in match_tag_start
    self.append_node(parsetree.Tag, keyword, attributes)
  File "/tmp/tmp/env/lib/python3.12/site-packages/mako/lexer.py", line 129, in append_node
    node = nodecls(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/tmp/env/lib/python3.12/site-packages/mako/parsetree.py", line 250, in __call__
    return type.__call__(cls, keyword, attributes, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/tmp/env/lib/python3.12/site-packages/mako/parsetree.py", line 380, in __init__
    super().__init__(
  File "/tmp/tmp/env/lib/python3.12/site-packages/mako/parsetree.py", line 299, in __init__
    self._parse_attributes(expressions, nonexpressions)
  File "/tmp/tmp/env/lib/python3.12/site-packages/mako/parsetree.py", line 330, in _parse_attributes
    code = ast.PythonCode(
           ^^^^^^^^^^^^^^^
  File "/tmp/tmp/env/lib/python3.12/site-packages/mako/ast.py", line 42, in __init__
    expr = pyparser.parse(code.lstrip(), "exec", **exception_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/tmp/env/lib/python3.12/site-packages/mako/pyparser.py", line 38, in parse
    raise exceptions.SyntaxException(
mako.exceptions.SyntaxException: (SyntaxError) unmatched '}' (<unknown>, line 1) ('foo}${bar') at line: 1 char: 1

Changing it to this works fine, though:

mytemplate = Template('<%include file="${foo}"/>', lookup=mylookup)
print(mytemplate.render(foo='foobar'))

Maybe related to #397?

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions