Commit 8bd69dca authored by Fred Drake's avatar Fred Drake

Some small optimizations:

SafeMapping.has_get():
    Simplify in order to reduce the number of Python bytecodes.  There
    is no longer a branch, which isn't strictly necessary.

Context.evaluateStructure(), .evaluateMacro():
    Since these methods are essentially synonyms for evaluate(), avoid
    the extra method lookup & call by simply making them aliases.

Context.evaluate():
    Re-arrange the try/except/except to avoid some of the exception
    catching by moving part of the try clause into an else clause.
parent 88f00992
...@@ -87,11 +87,13 @@ ...@@ -87,11 +87,13 @@
An implementation of a generic TALES engine An implementation of a generic TALES engine
""" """
__version__='$Revision: 1.14 $'[11:-2] __version__='$Revision: 1.15 $'[11:-2]
import re, sys, ZTUtils import re, sys, ZTUtils
from MultiMapping import MultiMapping from MultiMapping import MultiMapping
StringType = type('')
NAME_RE = r"[a-zA-Z][a-zA-Z0-9_]*" NAME_RE = r"[a-zA-Z][a-zA-Z0-9_]*"
_parse_expr = re.compile(r"(%s):(.*)" % NAME_RE).match _parse_expr = re.compile(r"(%s):(.*)" % NAME_RE).match
_valid_name = re.compile('%s$' % NAME_RE).match _valid_name = re.compile('%s$' % NAME_RE).match
...@@ -147,10 +149,7 @@ class SafeMapping(MultiMapping): ...@@ -147,10 +149,7 @@ class SafeMapping(MultiMapping):
def has_get(self, key, _marker=[]): def has_get(self, key, _marker=[]):
v = self.get(key, _marker) v = self.get(key, _marker)
if v is _marker: return v is not _marker, v
return 0, None
else:
return 1, v
class Iterator(ZTUtils.Iterator): class Iterator(ZTUtils.Iterator):
def __init__(self, name, seq, context): def __init__(self, name, seq, context):
...@@ -276,28 +275,29 @@ class Context: ...@@ -276,28 +275,29 @@ class Context:
self._current_ctxts['repeat'][name] = it self._current_ctxts['repeat'][name] = it
return it return it
def evaluate(self, expression): def evaluate(self, expression,
if type(expression) is type(''): isinstance=isinstance, StringType=StringType):
if isinstance(expression, StringType):
expression = self._engine.compile(expression) expression = self._engine.compile(expression)
try: try:
v = expression(self) v = expression(self)
if isinstance(v, Exception):
raise v
return v
except TALESError: except TALESError:
raise raise
except: except:
if type(sys.exc_info()[0]) is type(''): if isinstance(sys.exc_info()[0], StringType):
raise raise
raise TALESError, (`expression`, sys.exc_info()), sys.exc_info()[2] raise TALESError, (`expression`, sys.exc_info()), sys.exc_info()[2]
else:
if isinstance(v, Exception):
raise v
return v
evaluateValue = evaluate evaluateValue = evaluate
def evaluateBoolean(self, expr): def evaluateBoolean(self, expr):
bool = self.evaluate(expr) return not not self.evaluate(expr)
return not not bool
def evaluateText(self, expr): def evaluateText(self, expr, None=None):
text = self.evaluate(expr) text = self.evaluate(expr)
if text is Default or text is None: if text is Default or text is None:
return text return text
...@@ -305,10 +305,12 @@ class Context: ...@@ -305,10 +305,12 @@ class Context:
def evaluateStructure(self, expr): def evaluateStructure(self, expr):
return self.evaluate(expr) return self.evaluate(expr)
evaluateStructure = evaluate
def evaluateMacro(self, expr): def evaluateMacro(self, expr):
# XXX Should return None or a macro definition # XXX Should return None or a macro definition
return self.evaluate(expr) return self.evaluate(expr)
evaluateMacro = evaluate
def getTALESError(self): def getTALESError(self):
return TALESError return TALESError
...@@ -326,8 +328,3 @@ class SimpleExpr: ...@@ -326,8 +328,3 @@ class SimpleExpr:
def __repr__(self): def __repr__(self):
return '<SimpleExpr %s %s>' % (self._name, `self._expr`) return '<SimpleExpr %s %s>' % (self._name, `self._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