From 33782e74078d7989467db9fedaaa994474d06e4c Mon Sep 17 00:00:00 2001
From: Mike Bayer <mike_mp@zzzcomputing.com>
Date: Thu, 8 Nov 2007 19:25:41 +0000
Subject: [PATCH] - fixed bug where local.get_namespace() could put an  
 incorrect "self" in the current context

---
 CHANGES             |  2 ++
 lib/mako/runtime.py |  2 +-
 test/namespace.py   | 47 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 50 insertions(+), 1 deletion(-)

diff --git a/CHANGES b/CHANGES
index 9f1efe9..75c1543 100644
--- a/CHANGES
+++ b/CHANGES
@@ -4,6 +4,8 @@
   to the %call function itself (propigates to the inner
   calls too, this is a slight side effect which previously
   existed anyway)
+- fixed bug where local.get_namespace() could put an 
+  incorrect "self" in the current context
 
 0.1.9
 - filters.Decode filter can also accept a non-basestring
diff --git a/lib/mako/runtime.py b/lib/mako/runtime.py
index 628496e..d08a209 100644
--- a/lib/mako/runtime.py
+++ b/lib/mako/runtime.py
@@ -134,7 +134,7 @@ class Namespace(object):
         if self.context.namespaces.has_key(key):
             return self.context.namespaces[key]
         else:
-            ns = Namespace(uri, self.context, templateuri=uri, calling_uri=self._templateuri) 
+            ns = Namespace(uri, self.context._copy(), templateuri=uri, calling_uri=self._templateuri) 
             self.context.namespaces[key] = ns
             return ns
     
diff --git a/test/namespace.py b/test/namespace.py
index f040eb6..9be445c 100644
--- a/test/namespace.py
+++ b/test/namespace.py
@@ -210,6 +210,53 @@ class NamespaceTest(unittest.TestCase):
             "this is ns.html->bar"
         ]
     
+    def test_dont_pollute_self(self):
+        # test that get_namespace() doesn't modify the original context
+        # incompatibly
+        
+        collection = lookup.TemplateLookup()
+        collection.put_string("base.html", """
+
+        <%def name="foo()">
+        <%
+            foo = local.get_namespace("foo.html")
+        %>
+        </%def>
+
+        name: ${self.name}
+        name via bar: ${bar()}
+
+        ${next.body()}
+
+        name: ${self.name}
+        name via bar: ${bar()}
+        <%def name="bar()">
+            ${self.name}
+        </%def>
+
+
+        """)
+
+        collection.put_string("page.html", """
+        <%inherit file="base.html"/>
+
+        ${self.foo()}
+
+        hello world
+
+        """)
+
+        collection.put_string("foo.html", """<%inherit file="base.html"/>""")
+        assert result_lines(collection.get_template("page.html").render()) == [
+            "name: self:page.html",
+            "name via bar:",
+            "self:page.html",
+            "hello world",
+            "name: self:page.html",
+            "name via bar:",
+            "self:page.html"
+        ]
+        
     def test_inheritance(self):
         """test namespace initialization in a base inherited template that doesnt otherwise access the namespace"""
         collection = lookup.TemplateLookup()
-- 
GitLab