[ZPT] CVS: Packages/Products/PageTemplates (Products/DC/PageTemplates) - TALES.py:1.15

fred@digicool.com fred@digicool.com
Tue, 12 Jun 2001 17:44:51 -0400 (EDT)


Update of /cvs-repository/Packages/Products/PageTemplates
In directory korak.digicool.com:/tmp/cvs-serv2473

Modified Files:
	TALES.py 
Log Message:
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.



--- Updated File TALES.py in package Packages/Products/PageTemplates --
--- TALES.py	2001/06/07 14:51:45	1.14
+++ TALES.py	2001/06/12 21:44:50	1.15
@@ -92,6 +92,8 @@
 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 @@
 
     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 @@
         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 @@
 
     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
@@ -325,9 +327,4 @@
         return self._name, self._expr
     def __repr__(self):
         return '<SimpleExpr %s %s>' % (self._name, `self._expr`)
-
-
-
-
-