Skip to content

1.3.4 breaks includes with two variables #401

Closed

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!

Activity

  1. zzzeek commented on May 14, 2024

    @zzzeek
    Member

    Yes I was kind of concerned about this new change, but we couldnt get anything to show it failing. I'm going to revert it right now and we can add this new case as a test.

    1.3.4 is yanked

  2. sqla-tester commented on May 14, 2024

    @sqla-tester
    Collaborator

    Mike Bayer has proposed a fix for this issue in the main branch:

    Revert "Update parsetree.py removed "?" from for x in re.compile(r"(${.+})" …" https://gerrit.sqlalchemy.org/c/sqlalchemy/mako/+/5304

  3. zzzeek commented on May 14, 2024

    @zzzeek
    Member

    thanks for the quick reporting, 1.3.5 is released

  4. vain commented on May 14, 2024

    @vain
    Author

    Alright, thanks for the fix! :)

  5. added a commit that references this issue on May 15, 2024
    dcc211c
  6. sqla-tester commented on Dec 3, 2024

    @sqla-tester
    Collaborator

    Hai Zhu has proposed a fix for this issue in the main branch:

    Support the direct passing of dictionary literals. https://gerrit.sqlalchemy.org/c/sqlalchemy/mako/+/5558

  7. added a commit that references this issue on Dec 3, 2024
    8606516
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions