Issues

Issue #109 resolved

Cannot define locals in def in namespace

guest
created an issue

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:

  1. !python 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

Comments (5)

  1. Philip Jenvey

    Here's the compiled version, oddly enough there's 2 add functions

    1. !python from mako import runtime, filters, cache UNDEFINED = runtime.UNDEFINED M_dict_builtin = dict M_locals_builtin = locals _magic_number = 5 _modified_time = 1264106163.958884 _template_filename='foo.mako' _template_uri='foo.mako' _template_cache=cache.Cache(name, _modified_time) _source_encoding=None _exports = []

    def _mako_get_namespace(context, name): try: return context.namespaces[(name, name)] except KeyError: _mako_generate_namespaces(context) return context.namespaces[(name, name)] def _mako_generate_namespaces(context):

    1. SOURCE LINE 1 def make_namespace(): def add(x,y): context.caller_stack._push_frame() try: M_writer = context.writer()
    2. SOURCE LINE 2 M_writer(u'\n ')
    3. SOURCE LINE 3

    result = x + y

    M_locals.update(M_dict_builtin([(M_key, M_locals_builtin()[M_key]) for M_key in ['result'] if M_key in M_locals_builtin()]))

    1. SOURCE LINE 5 M_writer(u'\n ')
    2. SOURCE LINE 6 M_writer(unicode(result)) M_writer(u'\n ') return '' finally: context.caller_stack._pop_frame() return [add] ns = runtime.Namespace(u'math', context._clean_inheritance_tokens(), templateuri=None, callables=make_namespace(), calling_uri=_template_uri, module=None) context.namespaces[(name, u'math')] = ns

    def render_body(context,pageargs): context.caller_stack._push_frame() try: M_locals = M_dict_builtin(pageargs=pageargs) global_x = context.get('global_x', UNDEFINED) global_y = context.get('global_y', UNDEFINED) def add(x,y): context.caller_stack._push_frame() try: M_writer = context.writer()

    1. SOURCE LINE 2 M_writer(u'\n ')
    2. SOURCE LINE 3

    result = x + y

    M_locals.update(M_dict_builtin([(M_key, M_locals_builtin()[M_key]) for M_key in ['result'] if M_key in M_locals_builtin()]))

    1. SOURCE LINE 5 M_writer(u'\n ')
    2. SOURCE LINE 6 M_writer(unicode(result)) M_writer(u'\n ') return '' finally: context.caller_stack._pop_frame() math = _mako_get_namespace(context, 'math') M_writer = context.writer()
    3. SOURCE LINE 8 M_writer(u'\n')
    4. SOURCE LINE 9 M_writer(unicode(math.add(global_x,global_y))) M_writer(u'\n') return '' finally: context.caller_stack._pop_frame()
  2. Log in to comment