# HG changeset patch # User cmlenz # Date 1207516993 0 # Node ID 0af8f63514b8b71d65d960bdaede6201a9ee3624 # Parent 9dd5f370a70e1296b5d5b1f5b58699a29a2a5198 Partially revert [717] to retain compatibility with Python 2.3. diff --git a/genshi/template/directives.py b/genshi/template/directives.py --- a/genshi/template/directives.py +++ b/genshi/template/directives.py @@ -24,8 +24,7 @@ from genshi.template.base import TemplateRuntimeError, TemplateSyntaxError, \ EXPR, _apply_directives, _eval_expr, \ _exec_suite -from genshi.template.eval import Expression, Suite, ExpressionASTTransformer, \ - _parse +from genshi.template.eval import Expression, ExpressionASTTransformer, _parse __all__ = ['AttrsDirective', 'ChooseDirective', 'ContentDirective', 'DefDirective', 'ForDirective', 'IfDirective', 'MatchDirective', @@ -700,27 +699,30 @@ 42 7 52 """ - __slots__ = ['suite'] + __slots__ = ['vars'] def __init__(self, value, template, namespaces=None, lineno=-1, offset=-1): Directive.__init__(self, None, template, namespaces, lineno, offset) + self.vars = [] + value = value.strip() try: - self.suite = Suite(value, template.filepath, lineno, - lookup=template.lookup, - xform=ExpressionASTTransformer) + ast = _parse(value, 'exec').node + for node in ast.nodes: + if isinstance(node, compiler.ast.Discard): + continue + elif not isinstance(node, compiler.ast.Assign): + raise TemplateSyntaxError('only assignment allowed in ' + 'value of the "with" directive', + template.filepath, lineno, offset) + self.vars.append(([_assignment(n) for n in node.nodes], + Expression(node.expr, template.filepath, + lineno, lookup=template.lookup))) except SyntaxError, err: err.msg += ' in expression "%s" of "%s" directive' % (value, self.tagname) raise TemplateSyntaxError(err, template.filepath, lineno, offset + (err.offset or 0)) - for node in self.suite.ast.node.nodes: - if not isinstance(node, (compiler.ast.Discard, - compiler.ast.Assign)): - raise TemplateSyntaxError('only assignment allowed in value of ' - 'the "with" directive', - template.filepath, lineno, offset) - def attach(cls, template, stream, value, namespaces, pos): if type(value) is dict: value = value.get('vars') @@ -729,8 +731,12 @@ attach = classmethod(attach) def __call__(self, stream, directives, ctxt, **vars): - ctxt.push({}) - _exec_suite(self.suite, ctxt, **vars) + frame = {} + ctxt.push(frame) + for targets, expr in self.vars: + value = _eval_expr(expr, ctxt, **vars) + for assign in targets: + assign(frame, value) for event in _apply_directives(stream, directives, ctxt, **vars): yield event ctxt.pop()