Skip to content

Cannot define locals in def in namespace #109

@sqlalchemy-bot

Description

@sqlalchemy-bot

Migrated issue, originally created by Anonymous

Maybe I shouldn't be doing this or I'm doing something wrong BUT I'm trying to define a local variable in a template function that is nested within a namespace. The error goes away if I do not try to define a local variable.

Here is a cooked up example of the offending code:

from mako.template import Template


t = Template('''
<%namespace name="math">
    <%def name="add(x, y)">
        <%
        result = x + y
        %>
        ${result}
    </%def>
</%namespace>
${math.add(global_x,global_y)}
''')
print t.render(global_x=1,global_y=2)

Here is the resulting traceback:

Traceback (most recent call last):
  File "makobug.py", line 15, in <module>
    print t.render(global_x=1,global_y=2)
  File "/home/ian/workspace/makobug/lib/python2.5/site-packages/Mako-0.2.5dev_r445-py2.5.egg/mako/template.py", line 133, in render
    return runtime._render(self, self.callable_, args, data)
  File "/home/ian/workspace/makobug/lib/python2.5/site-packages/Mako-0.2.5dev_r445-py2.5.egg/mako/runtime.py", line 364, in _render
    _render_context(template, callable_, context, *args, **_kwargs_for_callable(callable_, data))
  File "/home/ian/workspace/makobug/lib/python2.5/site-packages/Mako-0.2.5dev_r445-py2.5.egg/mako/runtime.py", line 381, in _render_context
    _exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
  File "/home/ian/workspace/makobug/lib/python2.5/site-packages/Mako-0.2.5dev_r445-py2.5.egg/mako/runtime.py", line 414, in _exec_template
    callable_(context, *args, **kwargs)
  File "memory:0xb7c3c88cL", line 80, in render_body
  File "memory:0xb7c3c88cL", line 34, in add
NameError: global name '__M_locals' is not defined

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions