sqlalchemy / mako

Mako Templates for Python
https://www.makotemplates.org
MIT License
353 stars 60 forks source link

Accessing an attribute or an index in a dictionnary comprehension trigger an error with the mako version 1.3.3 #398

Closed sgranjoux closed 3 months ago

sgranjoux commented 3 months ago

Here is an example

from mako.template import Template

text = """
<%
    mydict = { 'foo': 1 }

    def getkeys(x):
        return { k[0]: 0 for k in x.keys() }
%>

${ ','.join( getkeys(mydict) ) }
"""

tmpl = Template(text=text, strict_undefined=True)
out = tmpl.render()
print(out)

This produces the following error:

Traceback (most recent call last):
  File "/home/sgr/Programmation/mako/test3.py", line 16, in <module>
    tmpl = Template(text=text, strict_undefined=True)
  File "/home/sgr/Programmation/mako/mako/template.py", line 299, in __init__
    (code, module) = _compile_text(self, text, filename)
  File "/home/sgr/Programmation/mako/mako/template.py", line 676, in _compile_text
    source, lexer = _compile(
  File "/home/sgr/Programmation/mako/mako/template.py", line 656, in _compile
    node = lexer.parse()
  File "/home/sgr/Programmation/mako/mako/lexer.py", line 248, in parse
    if self.match_python_block():
  File "/home/sgr/Programmation/mako/mako/lexer.py", line 404, in match_python_block
    self.append_node(
  File "/home/sgr/Programmation/mako/mako/lexer.py", line 129, in append_node
    node = nodecls(*args, **kwargs)
  File "/home/sgr/Programmation/mako/mako/parsetree.py", line 158, in __init__
    self.code = ast.PythonCode(text, **self.exception_kwargs)
  File "/home/sgr/Programmation/mako/mako/ast.py", line 47, in __init__
    f.visit(expr)
  File "/home/sgr/Programmation/mako/mako/_ast_util.py", line 136, in visit
    return self.generic_visit(node)
  File "/home/sgr/Programmation/mako/mako/_ast_util.py", line 144, in generic_visit
    self.visit(item)
  File "/home/sgr/Programmation/mako/mako/_ast_util.py", line 135, in visit
    return f(node)
  File "/home/sgr/Programmation/mako/mako/pyparser.py", line 91, in visit_FunctionDef
    self._visit_function(node, False)
  File "/home/sgr/Programmation/mako/mako/pyparser.py", line 138, in _visit_function
    self.visit(n)
  File "/home/sgr/Programmation/mako/mako/_ast_util.py", line 136, in visit
    return self.generic_visit(node)
  File "/home/sgr/Programmation/mako/mako/_ast_util.py", line 146, in generic_visit
    self.visit(value)
  File "/home/sgr/Programmation/mako/mako/_ast_util.py", line 135, in visit
    return f(node)
  File "/home/sgr/Programmation/mako/mako/pyparser.py", line 107, in visit_DictComp
    self.visit(node.elt)
AttributeError: 'DictComp' object has no attribute 'elt'

I am running Python 3.10.12 and Mako 1.3.3. It was working with Mako 1.3.2.

sgranjoux commented 3 months ago

This regression has been introduced by this commit https://github.com/sqlalchemy/mako/commit/e2606d5b37e9a98709147f198de59050869436b4

zzzeek commented 3 months ago

whew OK it's not a regex issue, should be easy to fix unless you have a PR in mind?

sqla-tester commented 3 months ago

Sébastien Granjoux has proposed a fix for this issue in the main branch:

Fix support comprehensions inside functions when use strict_undefined… https://gerrit.sqlalchemy.org/c/sqlalchemy/mako/+/5322