Commit 747e87e4 authored by Guido van Rossum's avatar Guido van Rossum

Added proper scoping for local variables.

I chose semantics that make all variables defined in outer scopes
(including globals) visible as locals, unless overridden by assignment
in inner scope.
parent 2b66c667
......@@ -93,12 +93,21 @@ from TALVisitor import NAME_RE
class DummyEngine:
def __init__(self):
self.locals = {}
self.globals = {}
dict = {}
self.locals = self.globals = dict
self.stack = [dict]
# XXX scopes
def beginScope(self):
self.stack.append(self.locals)
def endScope(self):
assert len(self.stack) > 1, "more endScope() than beginScope() calls"
self.locals = self.stack.pop()
def setLocal(self, name, value):
if self.locals is self.stack[-1]:
# Unmerge this scope's locals from previous scope of first set
self.locals = self.locals.copy()
self.locals[name] = value
def setGlobal(self, name, value):
......
......@@ -110,8 +110,10 @@ class TALVisitor(CopyingDOMVisitor):
self.engine = engine
def visitElement(self, node):
self.engine.beginScope()
if not self.checkTAL(node):
CopyingDOMVisitor.visitElement(self, node)
self.engine.endScope()
def checkTAL(self, node):
# Do TAL expansion. Return value is 1 if node expansion
......
<?xml version="1.0" ?>
<p xmlns:z="http://xml.zope.org/namespaces/tal">
<span z:define="local x as str:hello brave new world">
<span z:insert="text local:x">outer variable x, first appearance</span>
<span z:define="local x as str:goodbye cruel world">
<span z:insert="text local:x">inner variable x</span>
</span>
<span z:insert="text local:x">outer variable x, second appearance</span>
</span>
</p>
<?xml version="1.0" ?>
<p xmlns:z="http://xml.zope.org/namespaces/tal">
<span z:define="local x as str:hello brave new world">
<span z:insert="text local:x">outer variable x, first appearance</span>
<span z:define="local x as str:goodbye cruel world">
<span z:insert="text local:x">inner variable x</span>
</span>
<span z:insert="text local:x">outer variable x, second appearance</span>
</span>
</p>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment