Issues

Issue #83 resolved

Behavior of Builtins

guest
created an issue

Currently there is some weird behavior regarding identifiers named after builtins. I notived it when using a Mako template written for an application running on Python 2.4 with Python 2.5 which introduced "all" as builtin. The template used that as identifier and after the upgrade to 2.5 the template broke.

Turns out that currently SQLAlchemy doesn't pull identifiers named after builtins from the context. This appears to be expected behavior as there is a unittest for that which apparently doesn't fail or at least not in that situation.

Currently the template "${id}" generates this code:

  1. !python from mako import runtime, filters, cache UNDEFINED = runtime.UNDEFINED _magic_number = 2 _modified_time = 1211488200.114598 _template_filename=None _template_uri='memory:0x7077d0' _template_cache=cache.Cache(name, _modified_time) _source_encoding=None _exports = []

def render_body(context,pageargs): context.caller_stack._push_frame() try: M_locals = dict(pageargs=pageargs) M_writer = context.writer()

  1. SOURCE LINE 1 M_writer(unicode(id)) return '' finally: context.caller_stack._pop_frame()

I suggest generating this code instead:

  1. !python import builtin from mako import runtime, filters, cache UNDEFINED = runtime.UNDEFINED _magic_number = 2 _modified_time = 1211488200.114598 _template_filename=None _template_uri='memory:0x7077d0' _template_cache=cache.Cache(name, _modified_time) _source_encoding=None _exports = []

def render_body(context,pageargs): context.caller_stack._push_frame() try: M_locals = dict(pageargs=pageargs) id = context.get('id', builtin.id) M_writer = context.writer()

  1. SOURCE LINE 1 M_writer(unicode(id)) return '' finally: context.caller_stack._pop_frame()

While the lookup of the builtin will be slowed down a bit the actual usage of it is faster because it's a local variable then which isn't looked up in a dict.

I have a possible patch here: http://paste.pocoo.org/show/52822/

However that patch causes the current test for the old behavior to break. If the old behavior is really the desired one that should be documented at least.