Commit b27a4c42 authored by Guido van Rossum's avatar Guido van Rossum

Define TALESError, an exception class that carries another

exception+traceback around.  This is raised by evaluate().

Fix evaluate() to support string:, path:, not:, and exists:.  For
compatibility it still recognizes str: as an alias for string:, and
var:, global: and local: as aliases for path:.
parent 1c9c01ed
...@@ -87,12 +87,10 @@ Dummy TALES engine so that I can test out the TAL implementation. ...@@ -87,12 +87,10 @@ Dummy TALES engine so that I can test out the TAL implementation.
""" """
import re import re
import sys
import string import string
from TALDefs import NAME_RE, TALError from TALDefs import NAME_RE, TALError, TALESError
class TALESError(TALError):
pass
class DummyEngine: class DummyEngine:
...@@ -125,22 +123,30 @@ class DummyEngine: ...@@ -125,22 +123,30 @@ class DummyEngine:
if m: if m:
type, expr = m.group(1, 2) type, expr = m.group(1, 2)
else: else:
type = "var" type = "path"
expr = expression expr = expression
if type == "str": if type in ("string", "str"):
return expr return expr
if type == "local": if type in ("path", "var", "global", "local"):
return self.locals[string.strip(expr)]
if type == "global":
return self.globals[string.strip(expr)]
if type == "var":
expr = string.strip(expr) expr = string.strip(expr)
if self.locals.has_key(expr): if self.locals.has_key(expr):
return self.locals[expr] return self.locals[expr]
else: elif self.globals.has_key(expr):
return self.globals[expr] return self.globals[expr]
else:
raise TALESError("unknown variable: %s", expr)
if type == "not":
v = self.evaluate(expr)
return not v
if type == "exists":
return self.locals.has_key(expr) or self.globals.has_key(expr)
if type == "python": if type == "python":
return eval(expr, self.globals, self.locals) try:
return eval(expr, self.globals, self.locals)
except:
t, v, tb = info = sys.exc_info()
raise TALESError("evaluation error in %s" % `expr`,
info=sys.exc_info())
raise TALESError("unrecognized expression: " + `expression`) raise TALESError("unrecognized expression: " + `expression`)
def evaluateValue(self, expr): def evaluateValue(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