Skip to content

Behavior of Builtins #83

@sqlalchemy-bot

Description

@sqlalchemy-bot

Migrated issue, originally created by Anonymous

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:

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()
        # SOURCE LINE 1
        __M_writer(unicode(id))
        return ''
    finally:
        context.caller_stack._pop_frame()

I suggest generating this code instead:

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions