# HG changeset patch # User cmlenz # Date 1163090601 0 # Node ID f5ba52840cc98aa6ce3e2f1e2993ac71e4d27a74 # Parent 83c3d04bba9724f54e4fb6ebd19692742a332ebd inline branch: Merged [418], and take advantage of it in the inlining code. diff --git a/genshi/template/eval.py b/genshi/template/eval.py --- a/genshi/template/eval.py +++ b/genshi/template/eval.py @@ -83,6 +83,15 @@ self.code = _compile(ast.Expression(source), filename=filename, lineno=lineno) + def __eq__(self, other): + return (type(other) == Expression) and (self.code == other.code) + + def __hash__(self): + return hash(self.code) + + def __ne__(self, other): + return not self == other + def __repr__(self): return 'Expression(%r)' % self.source diff --git a/genshi/template/inline.py b/genshi/template/inline.py --- a/genshi/template/inline.py +++ b/genshi/template/inline.py @@ -107,10 +107,10 @@ else: for subkind, subdata, subpos in substream: if subkind is EXPR: - if subdata.source not in p_exprs: + if subdata not in p_exprs: ei[0] += 1 yield w('E%d = %r', ei[0], subdata) - p_exprs[subdata.source] = ei[0] + p_exprs[subdata] = ei[0] if tuple(sattrs) not in p_attrs: ai[0] += 1 @@ -118,25 +118,25 @@ p_attrs[tuple(sattrs)] = ai[0] elif kind is EXPR: - if data.source not in p_exprs: + if data not in p_exprs: ei[0] += 1 yield w('E%d = %r', ei[0], data) - p_exprs[data.source] = ei[0] + p_exprs[data] = ei[0] elif kind is SUB: directives, substream = data for directive in directives: if directive.expr: - if directive.expr.source not in p_exprs: + if directive.expr not in p_exprs: ei[0] += 1 yield w('E%d = %r', ei[0], directive.expr) - p_exprs[directive.expr.source] = ei[0] + p_exprs[directive.expr] = ei[0] elif hasattr(directive, 'vars'): for _, expr in directive.vars: - if expr.source not in p_exprs: + if expr not in p_exprs: ei[0] += 1 yield w('E%d = %r', ei[0], expr) - p_exprs[expr.source] = ei[0] + p_exprs[expr] = ei[0] elif hasattr(directive, 'path') and directive.path: yield w('P%d = %r', pi[0], directive.path) for line in _predecl(substream): @@ -163,8 +163,8 @@ if isinstance(directive, ContentDirective): ei[0] += 1 - yield w('for e in _expand(E%d.evaluate(ctxt), %r):', p_exprs[directive.expr.source], - (None, -1, -1)) + yield w('for e in _expand(E%d.evaluate(ctxt), %r):', + p_exprs[directive.expr], (None, -1, -1)) w.shift() lines = _apply(directives, stream) for line in lines: @@ -179,7 +179,7 @@ elif isinstance(directive, ForDirective): ei[0] += 1 - yield w('for v in E%d.evaluate(ctxt):', p_exprs[directive.expr.source]) + yield w('for v in E%d.evaluate(ctxt):', p_exprs[directive.expr]) w.shift() yield w('ctxt.push(%s)', _assign(directive.target)) for line in _apply(directives, stream): @@ -189,7 +189,7 @@ elif isinstance(directive, IfDirective): ei[0] += 1 - yield w('if E%d.evaluate(ctxt):', p_exprs[directive.expr.source]) + yield w('if E%d.evaluate(ctxt):', p_exprs[directive.expr]) w.shift() for line in _apply(directives, stream): yield line @@ -198,13 +198,13 @@ elif isinstance(directive, ReplaceDirective): ei[0] += 1 yield w('for e in _expand(E%d.evaluate(ctxt), %r): yield e', - p_exprs[directive.expr.source], + p_exprs[directive.expr], (None, -1, -1)) elif isinstance(directive, WithDirective): for targets, expr in directive.vars: ei[0] += 1 - yield w('v = E%d.evaluate(ctxt)', p_exprs[directive.expr.source]) + yield w('v = E%d.evaluate(ctxt)', p_exprs[directive.expr]) for node, _ in targets: yield w('ctxt.push(%s)', _assign(node)) for line in _apply(directives, stream): @@ -213,7 +213,7 @@ elif isinstance(directive, StripDirective): if directive.expr: - yield w('if E%d.evaluate(ctxt):', p_exprs[directive.expr.source]) + yield w('if E%d.evaluate(ctxt):', p_exprs[directive.expr]) w.shift() lines = _apply(directives, stream) previous = lines.next() @@ -244,7 +244,7 @@ if kind is EXPR: yield w('for e in _expand(E%d.evaluate(ctxt), %r): yield e', - p_exprs[data.source], pos) + p_exprs[data], pos) elif kind is START: tagname, attrs = data @@ -263,7 +263,7 @@ for subkind, subdata, subpos in value: if subkind is EXPR: parts.append('list(_expand_text(E%d.evaluate(ctxt)))' % - p_exprs[subdata.source]) + p_exprs[subdata]) elif subkind is TEXT: parts.append('[%r]' % subdata) yield w('v = [v for v in %s if v is not None]', diff --git a/genshi/template/tests/eval.py b/genshi/template/tests/eval.py --- a/genshi/template/tests/eval.py +++ b/genshi/template/tests/eval.py @@ -20,6 +20,16 @@ class ExpressionTestCase(unittest.TestCase): + def test_eq(self): + expr = Expression('x,y') + self.assertEqual(expr, Expression('x,y')) + self.assertNotEqual(expr, Expression('y, x')) + + def test_hash(self): + expr = Expression('x,y') + self.assertEqual(hash(expr), hash(Expression('x,y'))) + self.assertNotEqual(hash(expr), hash(Expression('y, x'))) + def test_name_lookup(self): self.assertEqual('bar', Expression('foo').evaluate({'foo': 'bar'})) self.assertEqual(id, Expression('id').evaluate({}, nocall=True))