changeset 480:2f46ae8deae2 stable-0.4.x

Ported [579] (for #116) to 0.4.x.
author cmlenz
date Mon, 14 May 2007 20:34:51 +0000
parents 3068d554de19
children ba71d343ac68
files ChangeLog genshi/template/directives.py genshi/template/tests/directives.py
diffstat 3 files changed, 32 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -25,6 +25,8 @@
    anyway, see ticket #113). This change does not affect expressions.
  * Numerous fixes for the execution of Python code in `<?python ?>` 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
--- a/genshi/template/directives.py
+++ b/genshi/template/directives.py
@@ -245,7 +245,7 @@
       </p>
     </div>
     """
-    __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
--- 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("""<doc xmlns:py="http://genshi.edgewall.org/">
+          <div py:def="f(*args, **kwargs)">
+            ${repr(args)}
+            ${repr(kwargs)}
+          </div>
+          ${f(1, 2, a=3, b=4)}
+        </doc>""")
+        self.assertEqual("""<doc>
+          <div>
+            [1, 2]
+            {'a': 3, 'b': 4}
+          </div>
+        </doc>""", str(tmpl.generate()))
+
 
 class ForDirectiveTestCase(unittest.TestCase):
     """Tests for the `py:for` template directive."""
Copyright (C) 2012-2017 Edgewall Software