Skip to content
New issue

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

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

Already on GitHub? # to your account

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

Closed
mgedmin opened this issue Dec 7, 2024 · 7 comments
Closed
Labels
bug Something isn't working lexer regression

Comments

@mgedmin
Copy link

mgedmin commented Dec 7, 2024

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.

@zzzeek
Copy link
Member

zzzeek commented Dec 7, 2024

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.

@zzzeek
Copy link
Member

zzzeek commented Dec 7, 2024

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()

@zzzeek
Copy link
Member

zzzeek commented Dec 7, 2024

this does it

        t1 = Template(
            "hello world ${x}", strict_undefined=True, default_filters=["str", "n"]
        )

        t1.render_unicode(x='hi')

@sqla-tester
Copy link
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

@zzzeek
Copy link
Member

zzzeek commented Dec 7, 2024

1.3.7 is yanked

@zzzeek
Copy link
Member

zzzeek commented Dec 7, 2024

1.3.8 is released

@mgedmin
Copy link
Author

mgedmin commented Dec 7, 2024

Wow that was fast. Thank you!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working lexer regression
Projects
None yet
Development

No branches or pull requests

3 participants