From dcee7369562d46d8b2fa68ce6ed399fa64189b06 Mon Sep 17 00:00:00 2001 From: Mike Bayer <mike_mp@zzzcomputing.com> Date: Tue, 21 Nov 2006 00:40:09 +0000 Subject: [PATCH] switched context from a "context = context.update()" model to a regular push()/pop() model --- lib/mako/codegen.py | 12 +++++++----- lib/mako/runtime.py | 7 ++----- test/component.py | 1 + 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/mako/codegen.py b/lib/mako/codegen.py index 1619426..900a0e5 100644 --- a/lib/mako/codegen.py +++ b/lib/mako/codegen.py @@ -139,7 +139,8 @@ class _GenerateRenderMethod(object): make_closure = len(identifiers.locally_declared) > 0 if make_closure: - self.printer.writeline("def %s(%s):" % (node.name, ",".join(['context'] + namedecls))) + self.printer.writeline("try:") + self.printer.writeline("context.push()") self.write_variable_declares(identifiers) for n in node.nodes: @@ -148,10 +149,11 @@ class _GenerateRenderMethod(object): self.printer.writeline(None) if make_closure: - namedecls = node.function_decl.get_argument_expressions(include_defaults=False) - self.printer.writeline("return %s(%s)" % (node.name, ",".join(["%s=%s" % (x,x) for x in ['context'] + namedecls]))) + self.printer.writeline("finally:") + self.printer.writeline("context.pop()") self.printer.writeline(None) - + self.printer.writeline(None) + def visitExpression(self, node): self.write_source_comment(node) self.printer.writeline("context.write(unicode(%s))" % node.text) @@ -169,7 +171,7 @@ class _GenerateRenderMethod(object): self.write_source_comment(node) self.printer.write_indented_block(node.text) # replace the context with a new one that contains all the variable assignments we have made - self.printer.writeline('context = context.update(%s)' % (",".join(["%s=%s" % (x, x) for x in node.declared_identifiers()]))) + self.printer.writeline('context.update(%s)' % (",".join(["%s=%s" % (x, x) for x in node.declared_identifiers()]))) def visitIncludeTag(self, node): self.write_source_comment(node) diff --git a/lib/mako/runtime.py b/lib/mako/runtime.py index 3409ff7..6c573f6 100644 --- a/lib/mako/runtime.py +++ b/lib/mako/runtime.py @@ -18,14 +18,11 @@ class Context(object): def update(self, **args): """produce a copy of this Context, updating the argument dictionary with the given keyword arguments.""" - x = self.stack[-1].copy() - x.update(args) - c = Context(self.with_template, self.buffer, **x) - return c + self.stack[-1].update(args) def push(self, **args): x = self.stack[-1].copy() x.update(args) - self.stack.append(args) + self.stack.append(x) def pop(self): self.stack.pop() diff --git a/test/component.py b/test/component.py index 39d51cc..4132284 100644 --- a/test/component.py +++ b/test/component.py @@ -310,6 +310,7 @@ class NestedComponentTest(unittest.TestCase): result = t.render(y=5) result = re.sub(r'[\s\n]+', ' ', result).strip() print result + assert result == "heres y: 5 now heres y 7 a, heres y: 7 a, now heres y: 10 a, heres b: b, heres y: 10 b, heres c: this is c b, heres y again: 19 heres y again: 7" def test_outer_scope(self): -- GitLab