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