[Zope-Checkins] CVS: Zope3/lib/python/Zope/TAL - TALInterpreter.py:1.82
Fred L. Drake, Jr.
fdrake@acm.org
Mon, 1 Jul 2002 17:49:59 -0400
Update of /cvs-repository/Zope3/lib/python/Zope/TAL
In directory cvs.zope.org:/tmp/cvs-serv17471
Modified Files:
TALInterpreter.py
Log Message:
Change the handling of temporary output streams so it only impacts
code that uses it; we don't need to add checks to every call of
interpret().
do_startTag(): Remove a local variable that's only used once.
=== Zope3/lib/python/Zope/TAL/TALInterpreter.py 1.81 => 1.82 ===
self.engine = engine # Execution engine (aka context)
self.Default = engine.getDefault()
- self.stream = stream or sys.stdout
- self._stream_write = self.stream.write
+ self._stream_stack = [stream or sys.stdout]
+ self.popStream()
self.debug = debug
self.wrap = wrap
self.metal = metal
@@ -117,11 +117,12 @@
self.i18nContext = TranslationContext()
def saveState(self):
- return (self.position, self.col, self.stream,
+ return (self.position, self.col, self.stream, self._stream_stack,
self.scopeLevel, self.level, self.i18nContext)
def restoreState(self, state):
- (self.position, self.col, self.stream, scopeLevel, level, i18n) = state
+ (self.position, self.col, self.stream,
+ self._stream_stack, scopeLevel, level, i18n) = state
self._stream_write = self.stream.write
assert self.level == level
while self.scopeLevel > scopeLevel:
@@ -131,7 +132,8 @@
self.i18nContext = i18n
def restoreOutputState(self, state):
- (dummy, self.col, self.stream, scopeLevel, level, i18n) = state
+ (dummy, self.col, self.stream,
+ self._stream_stack, scopeLevel, level, i18n) = state
self._stream_write = self.stream.write
assert self.level == level
assert self.scopeLevel == scopeLevel
@@ -168,6 +170,15 @@
self._stream_write("\n")
self.col = 0
+ def pushStream(self, newstream):
+ self._stream_stack.append(self.stream)
+ self.stream = newstream
+ self._stream_write = newstream.write
+
+ def popStream(self):
+ self.stream = self._stream_stack.pop()
+ self._stream_write = self.stream.write
+
def stream_write(self, s,
len=len):
self._stream_write(s)
@@ -179,15 +190,10 @@
bytecode_handlers = {}
- def interpret(self, program, tmpstream=None):
+ def interpret(self, program):
oldlevel = self.level
self.level = oldlevel + 1
handlers = self.dispatch
- if tmpstream:
- ostream = self.stream
- owrite = self._stream_write
- self.stream = tmpstream
- self._stream_write = tmpstream.write
try:
if self.debug:
for (opcode, args) in program:
@@ -202,9 +208,6 @@
handlers[opcode](self, args)
finally:
self.level = oldlevel
- if tmpstream:
- self.stream = ostream
- self._stream_write = owrite
def do_version(self, version):
assert version == TAL_VERSION
@@ -242,8 +245,7 @@
# for that case.
_stream_write = self._stream_write
_stream_write("<" + name)
- namelen = _len(name)
- col = self.col + namelen + 1
+ col = self.col + _len(name) + 1
wrap = self.wrap
align = col + 1
if align >= wrap/2:
@@ -476,7 +478,11 @@
state = self.saveState()
try:
tmpstream = StringIO()
- self.interpret(program, tmpstream)
+ self.pushStream(tmpstream)
+ try:
+ self.interpret(program)
+ finally:
+ self.popStream()
value = normalize(tmpstream.getvalue())
finally:
self.restoreState(state)
@@ -511,7 +517,11 @@
# Use a temporary stream to capture the interpretation of the
# subnodes, which should /not/ go to the output stream.
tmpstream = StringIO()
- self.interpret(stuff[1], tmpstream)
+ self.pushStream(tmpstream)
+ try:
+ self.interpret(stuff[1])
+ finally:
+ self.popStream()
# We only care about the evaluated contents if we need an implicit
# message id. All other useful information will be in the i18ndict on
# the top of the i18nStack.