Skip to content

Mako 1.3.7 breaks my code with "NameError: name 'n' is not defined" #415

Closed

Description

@mgedmin

The project in question: https://github.com/mgedmin/project-summary.

A week ago it was building fine, and today I got an email from GitHub Actions telling me about build failures. I've narrowed down the cause to a mako version change: 1.3.7 breaks my test suite, 1.3.6 works fine. The errors look like this:

summary.py:1027: in stylesheet_rules
    Template(rule, default_filters=['str', 'n']).render_unicode(
.tox/py312/lib/python3.12/site-packages/mako/template.py:443: in render_unicode
    return runtime._render(
.tox/py312/lib/python3.12/site-packages/mako/runtime.py:874: in _render
    _render_context(
.tox/py312/lib/python3.12/site-packages/mako/runtime.py:916: in _render_context
    _exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
.tox/py312/lib/python3.12/site-packages/mako/runtime.py:938: in _exec_template
    _render_error(template, context, compat.exception_as())
.tox/py312/lib/python3.12/site-packages/mako/runtime.py:948: in _render_error
    result = template.error_handler(context, error)
.tox/py312/lib/python3.12/site-packages/mako/runtime.py:936: in _exec_template
    callable_(context, *args, **kwargs)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

context = <mako.runtime.Context object at 0x718d12ba4410>
pageargs = {'column': <summary.DateColumn object at 0x718d12ba7e00>, 'css_class': 'date', 'discrim': '.date', 'page': <summary.Page object at 0x718d12b9e060>}, __M_caller = None
__M_locals = {'pageargs': {'column': <summary.DateColumn object at 0x718d12ba7e00>, 'css_class': 'date', 'discrim': '.date', 'page': <summary.Page object at 0x718d12b9e060>}}
column = <summary.DateColumn object at 0x718d12ba7e00>, page = <summary.Page object at 0x718d12b9e060>, css_class = 'date'
__M_writer = <built-in method append of collections.deque object at 0x718d12b5d120>

    def render_body(context,**pageargs):
        __M_caller = context.caller_stack._push_frame()
        try:
            __M_locals = __M_dict_builtin(pageargs=pageargs)
            try:
                column = context['column']
            except KeyError:
                raise NameError("'column' is not defined")
            try:
                page = context['page']
            except KeyError:
                raise NameError("'page' is not defined")
            try:
                css_class = context['css_class']
            except KeyError:
                raise NameError("'css_class' is not defined")
            __M_writer = context.writer()
            if column.align:
                __M_writer('      #')
>               __M_writer(n(str(page.name)))
        # memory:0x718d12b9d220 line 2 in render_body:
        #       #${page.name} th.${css_class},
                __M_writer(' th.')
                __M_writer(n(str(css_class)))
E               NameError: name 'n' is not defined

memory:0x718d12b9d220:34: NameError

I'm rendering the template using

def Template(*args, **kw) -> mako.template.Template:
    kw.setdefault('default_filters', ['h'])
    return mako.template.Template(error_handler=mako_error_handler,
                                  strict_undefined=True,
                                  *args, **kw)

...
                Template(rule, default_filters=['str', 'n']).render_unicode(
                    page=page,
                    column=self,
                    css_class=self.css_class,
                    discrim=discrim,
                )

Is this missing something?

This is probably related to the fix for #140, but I don't understand that issue nor what I'm doing wrong here. The release notes are also not very helpful.

Activity

  1. zzzeek commented on Dec 7, 2024

    @zzzeek
    Member

    with Mako I am just reverting these fixes, since they cannot predict real world cases, which here we will add and see if the contributor @cocolato wants to try again.

    the issue is that use of the default filter names has inconsistent behavior vs names that are not default filters.

  2. zzzeek commented on Dec 7, 2024

    @zzzeek
    Member

    I need to have an MCVE. Can you show me the arguments to make this script fail?

            t1 = Template(
                "hello world", strict_undefined=True, default_filters=["h"]
            )
    
            t1.render_unicode()
  3. zzzeek commented on Dec 7, 2024

    @zzzeek
    Member

    this does it

            t1 = Template(
                "hello world ${x}", strict_undefined=True, default_filters=["str", "n"]
            )
    
            t1.render_unicode(x='hi')
  4. sqla-tester commented on Dec 7, 2024

    @sqla-tester
    Collaborator

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

    Revert "Support passing custom filters with the same name as built-in flags" https://gerrit.sqlalchemy.org/c/sqlalchemy/mako/+/5579

  5. zzzeek commented on Dec 7, 2024

    @zzzeek
    Member

    1.3.7 is yanked

  6. zzzeek commented on Dec 7, 2024

    @zzzeek
    Member

    1.3.8 is released

  7. mgedmin commented on Dec 7, 2024

    @mgedmin
    Author

    Wow that was fast. Thank you!

  8. added a commit that references this issue on Dec 8, 2024
    4e2de73
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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions