Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

strict_undefined flag is incompatible with comprehensions inside functions #320

Closed
ks07 opened this issue Jun 19, 2020 · 1 comment
Closed
Labels
bug Something isn't working lexer

Comments

@ks07
Copy link

ks07 commented Jun 19, 2020

Using a list comprehension inside of a pure python function within a template will produce a NameError if the variable name introduced in the comprehension hasn't previously been defined, and strict_undefined=True is provided.

See the following contrived example:

from mako.template import Template

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

    ## Uncomment the following line to workaround the error
    ##k = None
    def getkeys(x):
        return [ k for k in x.keys() ]
%>

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

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

The following error is produced:

Traceback (most recent call last):
  File "memory:0x7fe265f30cc0", line 20, in render_body
  File "/home/george/.local/share/virtualenvs/mako_test-_JNqmfEZ/lib/python3.6/site-packages/mako/runtime.py", line 106, in __getitem__
    return compat_builtins.__dict__[key]
KeyError: 'k'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 17, in <module>
    out = tmpl.render()
  File "/home/george/.local/share/virtualenvs/mako_test-_JNqmfEZ/lib/python3.6/site-packages/mako/template.py", line 476, in render
    return runtime._render(self, self.callable_, args, data)
  File "/home/george/.local/share/virtualenvs/mako_test-_JNqmfEZ/lib/python3.6/site-packages/mako/runtime.py", line 883, in _render
    **_kwargs_for_callable(callable_, data)
  File "/home/george/.local/share/virtualenvs/mako_test-_JNqmfEZ/lib/python3.6/site-packages/mako/runtime.py", line 920, in _render_context
    _exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
  File "/home/george/.local/share/virtualenvs/mako_test-_JNqmfEZ/lib/python3.6/site-packages/mako/runtime.py", line 947, in _exec_template
    callable_(context, *args, **kwargs)
  File "memory:0x7fe265f30cc0", line 22, in render_body
NameError: 'k' is not defined

The above code sample works as expected if strict_undefined is set to false.

I am running Python 3.6.9 and Mako 1.1.3

@zzzeek zzzeek added bug Something isn't working lexer labels Jun 19, 2020
@zzzeek
Copy link
Member

zzzeek commented Jun 19, 2020

this has to do with Mako's scoping rules. I haven't worked on non-trivial bugs in Mako for many years and Mako is essentially unmaintained for issues like these at the moment. If you had insight on working on this issue I can accept PRs if they also include tests.

cocolato added a commit to cocolato/mako that referenced this issue Feb 6, 2024
Closes: sqlalchemy#386
Pull-request: sqlalchemy#386
Pull-request-sha: 7840ad3

Change-Id: I0591873a83837f8f35b0963c0536df1e2675012f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working lexer
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants