Commits

Mike Bayer committed f19509f

- fixed bug whereby an <%included> template with
<%page> args named the same as a __builtin__ would not
honor the default value specified in <%page> [ticket:93]

  • Participants
  • Parent commits e96c391

Comments (0)

Files changed (3)

   for each individual key, thus consuming all available 
   memory for an arbitrarily large number of distinct 
   keys.
+
+- fixed bug whereby an <%included> template with 
+  <%page> args named the same as a __builtin__ would not
+  honor the default value specified in <%page> [ticket:93]
   
 - fixed the html_error_template not handling tracebacks from
   normal .py files with a magic encoding comment [ticket:88]

File lib/mako/runtime.py

 
 from mako import exceptions, util
 import inspect, sys
-import __builtin__
 
 class Context(object):
     """provides runtime namespace, output buffer, and various callstacks for templates."""
     def __init__(self, buffer, **data):
         self._buffer_stack = [buffer]
-        self._data = dict(__builtin__.__dict__)
+        self._orig = data  # original data, minus the builtins
+        self._data = dict(__builtins__)     # the context data which includes builtins
         self._data.update(data)
         self._kwargs = data.copy()
         self._with_template = None
         c = Context.__new__(Context)
         c._buffer_stack = self._buffer_stack
         c._data = self._data.copy()
+        c._orig = self._orig
         c._kwargs = self._kwargs
         c._with_template = self._with_template
         c.namespaces = self.namespaces
     """locate the template from the given uri and include it in the current output."""
     template = _lookup_template(context, uri, calling_uri)
     (callable_, ctx) = _populate_self_namespace(context._clean_inheritance_tokens(), template)
-    callable_(ctx, **_kwargs_for_callable(callable_, context._data, **kwargs))
+    callable_(ctx, **_kwargs_for_callable(callable_, context._orig, **kwargs))
         
 def _inherit_from(context, uri, calling_uri):
     """called by the _inherit method in template modules to set up the inheritance chain at the start

File test/template.py

             id: ${id}
         """)
         assert flatten_result(template.render(id='some id', Exception='some exception')) == "exception: some exception id: some id"
+
+    def test_builtin_names_dont_clobber_defaults_in_includes(self):
+        lookup = TemplateLookup()
+        lookup.put_string("test.mako", 
+        """
+        <%include file="test1.mako"/>
+
+        """)
+
+        lookup.put_string("test1.mako", """
+        <%page args="id='foo'"/>
+
+        ${id}
+        """)
+
+        for template in ("test.mako", "test1.mako"):
+            assert flatten_result(lookup.get_template(template).render()) == "foo"
+            assert flatten_result(lookup.get_template(template).render(id=5)) == "5"
+            assert flatten_result(lookup.get_template(template).render(id=id)) == "<built-in function id>"
     
     def test_dict_locals(self):
         template = Template("""
             this is b.  ${a}, ${b}, ${c}
         """)
         assert flatten_result(lookup.get_template("a").render(a=7,b=8,i='b')) == "this is a this is b. 7, 8, 5"
-
+    
+    def test_within_ccall(self):
+        lookup = TemplateLookup()
+        lookup.put_string("a", """this is a""")
+        lookup.put_string("b", """
+        <%def name="bar()">
+            bar: ${caller.body()}
+            <%include file="a"/>
+        </%def>
+        """)
+        lookup.put_string("c", """
+        <%namespace name="b" file="b"/>
+        <%b:bar>
+            calling bar
+        </%b:bar>
+        """)
+        assert flatten_result(lookup.get_template("c").render()) == "bar: calling bar this is a"
+        
 class ControlTest(unittest.TestCase):
     def test_control(self):
         t = Template("""