# HG changeset patch # User cmlenz # Date 1179174822 0 # Node ID 94191f4233f3a042092566b17b57e6b723a06a27 # Parent 477fac8052e34c90daf5eef72de1b3678536f54d Apply patch for #116. diff --git a/ChangeLog b/ChangeLog --- a/ChangeLog +++ b/ChangeLog @@ -32,6 +32,8 @@ anyway, see ticket #113). This change does not affect expressions. * Numerous fixes for the execution of Python code in `` processing instructions (tickets #113 and #114). + * The `py:def` (and `#def`) directive now supports "star args" (i.e. `*args` + and `**kwargs`) in the function declaration (ticket #116). Version 0.4 diff --git a/genshi/template/directives.py b/genshi/template/directives.py --- a/genshi/template/directives.py +++ b/genshi/template/directives.py @@ -245,7 +245,7 @@

""" - __slots__ = ['name', 'args', 'defaults'] + __slots__ = ['name', 'args', 'star_args', 'dstar_args', 'defaults'] ATTRIBUTE = 'function' @@ -253,6 +253,8 @@ Directive.__init__(self, None, template, namespaces, lineno, offset) ast = _parse(args).node self.args = [] + self.star_args = None + self.dstar_args = None self.defaults = {} if isinstance(ast, compiler.ast.CallFunc): self.name = ast.node.name @@ -265,6 +267,10 @@ lookup=template.lookup) else: self.args.append(arg.name) + if ast.star_args: + self.star_args = ast.star_args.name + if ast.dstar_args: + self.dstar_args = ast.dstar_args.name else: self.name = ast.name @@ -283,6 +289,10 @@ else: val = self.defaults.get(name).evaluate(ctxt) scope[name] = val + if not self.star_args is None: + scope[self.star_args] = args + if not self.dstar_args is None: + scope[self.dstar_args] = kwargs ctxt.push(scope) for event in _apply_directives(stream, ctxt, directives): yield event diff --git a/genshi/template/tests/directives.py b/genshi/template/tests/directives.py --- a/genshi/template/tests/directives.py +++ b/genshi/template/tests/directives.py @@ -384,6 +384,25 @@ Hi, you! """, str(tmpl.generate())) + def test_function_with_star_args(self): + """ + Verify that a named template function using "star arguments" works as + expected. + """ + tmpl = MarkupTemplate(""" +
+ ${repr(args)} + ${repr(kwargs)} +
+ ${f(1, 2, a=3, b=4)} +
""") + self.assertEqual(""" +
+ [1, 2] + {'a': 3, 'b': 4} +
+
""", str(tmpl.generate())) + class ForDirectiveTestCase(unittest.TestCase): """Tests for the `py:for` template directive."""