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