Edgewall Software

Ticket #116: ticket116.diff

File ticket116.diff, 2.6 KB (added by cmlenz, 16 years ago)

Proposed patch

  • genshi/template/tests/directives.py

     
    384384                      Hi, you!
    385385        """, str(tmpl.generate()))
    386386
     387    def test_function_with_star_args(self):
     388        """
     389        Verify that a named template function using "star arguments" works as
     390        expected.
     391        """
     392        tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
     393          <div py:def="f(*args, **kwargs)">
     394            ${repr(args)}
     395            ${repr(kwargs)}
     396          </div>
     397          ${f(1, 2, a=3, b=4)}
     398        </doc>""")
     399        self.assertEqual("""<doc>
     400          <div>
     401            [1, 2]
     402            {'a': 3, 'b': 4}
     403          </div>
     404        </doc>""", str(tmpl.generate()))
    387405
     406
    388407class ForDirectiveTestCase(unittest.TestCase):
    389408    """Tests for the `py:for` template directive."""
    390409
  • genshi/template/directives.py

     
    245245      </p>
    246246    </div>
    247247    """
    248     __slots__ = ['name', 'args', 'defaults']
     248    __slots__ = ['name', 'args', 'star_args', 'dstar_args', 'defaults']
    249249
    250250    ATTRIBUTE = 'function'
    251251
     
    253253        Directive.__init__(self, None, template, namespaces, lineno, offset)
    254254        ast = _parse(args).node
    255255        self.args = []
     256        self.star_args = None
     257        self.dstar_args = None
    256258        self.defaults = {}
    257259        if isinstance(ast, compiler.ast.CallFunc):
    258260            self.name = ast.node.name
     
    265267                                                         lookup=template.lookup)
    266268                else:
    267269                    self.args.append(arg.name)
     270            if ast.star_args:
     271                self.star_args = ast.star_args.name
     272            if ast.dstar_args:
     273                self.dstar_args = ast.dstar_args.name
    268274        else:
    269275            self.name = ast.name
    270276
     
    283289                    else:
    284290                        val = self.defaults.get(name).evaluate(ctxt)
    285291                    scope[name] = val
     292            if not self.star_args is None:
     293                scope[self.star_args] = args
     294            if not self.dstar_args is None:
     295                scope[self.dstar_args] = kwargs
    286296            ctxt.push(scope)
    287297            for event in _apply_directives(stream, ctxt, directives):
    288298                yield event