Skip to content

Error with nested list comprehension when using strict_undefined=True #418

@sgranjoux

Description

@sgranjoux

Here is a failing sample

from mako.template import Template

text = """
<%
b = {}
bs = sorted(b, key=lambda b: sum(f for s in b.values() for f in s.values()))
%>
"""

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

This produces the following error

Traceback (most recent call last):
  File "memory:0x7164e2d38380", line 28, in render_body
  File "/usr/local/lib/python3.12/dist-packages/mako/runtime.py", line 106, in __getitem__
    return builtins.__dict__[key]
           ~~~~~~~~~~~~~~~~~^^^^^
KeyError: 's'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/sgr/Projects/Ganymede/digital/design/top_xt018_fx/titi.py", line 11, in <module>
    out = tmpl.render()
          ^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/mako/template.py", line 434, in render
    return runtime._render(self, self.callable_, args, data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/mako/runtime.py", line 874, in _render
    _render_context(
  File "/usr/local/lib/python3.12/dist-packages/mako/runtime.py", line 916, in _render_context
    _exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
  File "/usr/local/lib/python3.12/dist-packages/mako/runtime.py", line 943, in _exec_template
    callable_(context, *args, **kwargs)
  File "memory:0x7164e2d38380", line 30, in render_body
NameError: 's' is not defined

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinglexer

    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