Commit a93f1dd9 authored by Fred Drake's avatar Fred Drake

SafeMapping:

    The _push() and _pop() methods aren't really any different from
    the push() and pop() methods from the base MultiMap, so we can use
    them directly to avoid a lot of looks and method invocation
    overhead, and it's easier to tell from the code that they are just
    a renaming.

Context.beginScope(), .endScope():
    Move an attribute access out of the loop since the attribute isn't
    re-bound inside the loop.

Context.evaluateText():
    Re-phrase the condition in the if statement to allow the runtime
    to perform fewer global name lookups when possible, and avoid the
    tuple construction completely.
parent 27a7b5e4
......@@ -87,7 +87,7 @@
An implementation of a generic TALES engine
"""
__version__='$Revision: 1.13 $'[11:-2]
__version__='$Revision: 1.14 $'[11:-2]
import re, sys, ZTUtils
from MultiMapping import MultiMapping
......@@ -141,14 +141,11 @@ class SafeMapping(MultiMapping):
'''
__allow_access_to_unprotected_subobjects__ = 1
push = pop = None
def _push(self, ob):
MultiMapping.push(self, ob)
def _pop(self, *args):
if args:
return apply(MultiMapping.pop, (self,) + args)
else:
return MultiMapping.pop(self)
def has_get(self, key):
_push = MultiMapping.push
_pop = MultiMapping.pop
def has_get(self, key, _marker=[]):
v = self.get(key, _marker)
if v is _marker:
return 0, None
......@@ -253,18 +250,19 @@ class Context:
oldctxts = self._current_ctxts
self._ctxts_pushed.append(oldctxts)
self._current_ctxts = ctxts = {}
contexts = self.contexts
for ctxname in oldctxts.keys():
# Push fresh namespace on each local stack.
ctxts[ctxname] = ctx = {}
self.contexts[ctxname]._push(ctx)
contexts[ctxname]._push(ctx)
def endScope(self):
self._current_ctxts = ctxts = self._ctxts_pushed.pop()
# Pop the ones that were pushed at the beginning of the scope.
contexts = self.contexts
for ctxname in ctxts.keys():
ctx = self.contexts[ctxname]._pop()
# Make sure there's no circular garbage
ctx.clear()
# Pop, then make sure there's no circular garbage
contexts[ctxname]._pop().clear()
def setLocal(self, name, value):
self._current_ctxts['local'][name] = value
......@@ -301,9 +299,9 @@ class Context:
def evaluateText(self, expr):
text = self.evaluate(expr)
if text not in (None, Default):
text = str(text)
return text
if text is Default or text is None:
return text
return str(text)
def evaluateStructure(self, expr):
return self.evaluate(expr)
......
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