Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Cannot define locals in def in namespace #109

Closed
sqlalchemy-bot opened this issue May 14, 2009 · 7 comments
Closed

Cannot define locals in def in namespace #109

sqlalchemy-bot opened this issue May 14, 2009 · 7 comments
Labels
bug Something isn't working compiler low priority
Milestone

Comments

@sqlalchemy-bot
Copy link

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
@sqlalchemy-bot
Copy link
Author

Michael Bayer (@zzzeek) wrote:

thats a bug alright.....

@sqlalchemy-bot
Copy link
Author

Michael Bayer (@zzzeek) wrote:

tentatively putting this on 0.3 depending on how hard it is

@sqlalchemy-bot
Copy link
Author

Changes by Michael Bayer (@zzzeek):

  • set milestone to "0.3"

@sqlalchemy-bot
Copy link
Author

Philip Jenvey (@pjenvey) wrote:

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

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):
    # SOURCE LINE 1
    def make_namespace():
        def add(x,y):
            context.caller_stack._push_frame()
            try:
                __M_writer = context.writer()
                # SOURCE LINE 2
                __M_writer(u'\n        ')
                # 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()]))
                # SOURCE LINE 5
                __M_writer(u'\n        ')
                # 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()
                # SOURCE LINE 2
                __M_writer(u'\n        ')
                # 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()]))
                # SOURCE LINE 5
                __M_writer(u'\n        ')
                # 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()
        # SOURCE LINE 8
        __M_writer(u'\n')
        # SOURCE LINE 9
        __M_writer(unicode(math.add(global_x,global_y)))
        __M_writer(u'\n')
        return ''
    finally:
        context.caller_stack._pop_frame()

@sqlalchemy-bot
Copy link
Author

Michael Bayer (@zzzeek) wrote:

fixes and tests for both issues are in 876c371.

@sqlalchemy-bot
Copy link
Author

Changes by Michael Bayer (@zzzeek):

  • changed status to closed

@sqlalchemy-bot
Copy link
Author

Changes by Michael Bayer (@zzzeek):

  • removed labels: runtime
  • added labels: compiler

@sqlalchemy-bot sqlalchemy-bot added compiler low priority bug Something isn't working labels Nov 26, 2018
@sqlalchemy-bot sqlalchemy-bot added this to the 0.3 milestone Nov 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compiler low priority
Projects
None yet
Development

No branches or pull requests

1 participant