changeset 827:0319a8874510

Avoid varargs on internal functions in template processing for slightly better performance.
author cmlenz
date Fri, 13 Mar 2009 15:57:04 +0000
parents 9cf631c0eace
children e1c6163c5077
files genshi/template/base.py genshi/template/directives.py genshi/template/markup.py
diffstat 3 files changed, 28 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/genshi/template/base.py
+++ b/genshi/template/base.py
@@ -248,7 +248,7 @@
         """Pop the top-most scope from the stack."""
 
 
-def _apply_directives(stream, directives, ctxt, **vars):
+def _apply_directives(stream, directives, ctxt, vars):
     """Apply the given directives to the stream.
     
     :param stream: the stream the directives should be applied to
@@ -262,7 +262,8 @@
         stream = directives[0](iter(stream), directives[1:], ctxt, **vars)
     return stream
 
-def _eval_expr(expr, ctxt, **vars):
+
+def _eval_expr(expr, ctxt, vars=None):
     """Evaluate the given `Expression` object.
     
     :param expr: the expression to evaluate
@@ -278,7 +279,8 @@
         ctxt.pop()
     return retval
 
-def _exec_suite(suite, ctxt, **vars):
+
+def _exec_suite(suite, ctxt, vars=None):
     """Execute the given `Suite` object.
     
     :param suite: the code suite to execute
@@ -538,7 +540,7 @@
                 yield kind, (tag, Attrs(new_attrs)), pos
 
             elif kind is EXPR:
-                result = _eval_expr(data, ctxt, **vars)
+                result = _eval_expr(data, ctxt, vars)
                 if result is not None:
                     # First check for a string, otherwise the iterable test
                     # below succeeds, and the string will be chopped up into
@@ -555,12 +557,12 @@
                         yield TEXT, unicode(result), pos
 
             elif kind is EXEC:
-                _exec_suite(data, ctxt, **vars)
+                _exec_suite(data, ctxt, vars)
 
             elif kind is SUB:
                 # This event is a list of directives and a list of nested
                 # events to which those directives should be applied
-                substream = _apply_directives(data[1], data[0], ctxt, **vars)
+                substream = _apply_directives(data[1], data[0], ctxt, vars)
                 for event in self._flatten(substream, ctxt, **vars):
                     yield event
 
--- a/genshi/template/directives.py
+++ b/genshi/template/directives.py
@@ -166,7 +166,7 @@
     def __call__(self, stream, directives, ctxt, **vars):
         def _generate():
             kind, (tag, attrib), pos  = stream.next()
-            attrs = _eval_expr(self.expr, ctxt, **vars)
+            attrs = _eval_expr(self.expr, ctxt, vars)
             if attrs:
                 if isinstance(attrs, Stream):
                     try:
@@ -182,7 +182,7 @@
             for event in stream:
                 yield event
 
-        return _apply_directives(_generate(), directives, ctxt, **vars)
+        return _apply_directives(_generate(), directives, ctxt, vars)
 
 
 class ContentDirective(Directive):
@@ -299,14 +299,14 @@
                     if name in kwargs:
                         val = kwargs.pop(name)
                     else:
-                        val = _eval_expr(self.defaults.get(name), ctxt, **vars)
+                        val = _eval_expr(self.defaults.get(name), ctxt, vars)
                     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, directives, ctxt, **vars):
+            for event in _apply_directives(stream, directives, ctxt, vars):
                 yield event
             ctxt.pop()
         function.__name__ = self.name
@@ -356,7 +356,7 @@
                                                namespaces, pos)
 
     def __call__(self, stream, directives, ctxt, **vars):
-        iterable = _eval_expr(self.expr, ctxt, **vars)
+        iterable = _eval_expr(self.expr, ctxt, vars)
         if iterable is None:
             return
 
@@ -366,7 +366,7 @@
         for item in iterable:
             assign(scope, item)
             ctxt.push(scope)
-            for event in _apply_directives(stream, directives, ctxt, **vars):
+            for event in _apply_directives(stream, directives, ctxt, vars):
                 yield event
             ctxt.pop()
 
@@ -397,9 +397,9 @@
                                               namespaces, pos)
 
     def __call__(self, stream, directives, ctxt, **vars):
-        value = _eval_expr(self.expr, ctxt, **vars)
+        value = _eval_expr(self.expr, ctxt, vars)
         if value:
-            return _apply_directives(stream, directives, ctxt, **vars)
+            return _apply_directives(stream, directives, ctxt, vars)
         return []
 
 
@@ -527,7 +527,7 @@
 
     def __call__(self, stream, directives, ctxt, **vars):
         def _generate():
-            if _eval_expr(self.expr, ctxt, **vars):
+            if _eval_expr(self.expr, ctxt, vars):
                 stream.next() # skip start tag
                 previous = stream.next()
                 for event in stream:
@@ -536,7 +536,7 @@
             else:
                 for event in stream:
                     yield event
-        return _apply_directives(_generate(), directives, ctxt, **vars)
+        return _apply_directives(_generate(), directives, ctxt, vars)
 
     @classmethod
     def attach(cls, template, stream, value, namespaces, pos):
@@ -597,9 +597,9 @@
     def __call__(self, stream, directives, ctxt, **vars):
         info = [False, bool(self.expr), None]
         if self.expr:
-            info[2] = _eval_expr(self.expr, ctxt, **vars)
+            info[2] = _eval_expr(self.expr, ctxt, vars)
         ctxt._choice_stack.append(info)
-        for event in _apply_directives(stream, directives, ctxt, **vars):
+        for event in _apply_directives(stream, directives, ctxt, vars):
             yield event
         ctxt._choice_stack.pop()
 
@@ -638,16 +638,16 @@
         if info[1]:
             value = info[2]
             if self.expr:
-                matched = value == _eval_expr(self.expr, ctxt, **vars)
+                matched = value == _eval_expr(self.expr, ctxt, vars)
             else:
                 matched = bool(value)
         else:
-            matched = bool(_eval_expr(self.expr, ctxt, **vars))
+            matched = bool(_eval_expr(self.expr, ctxt, vars))
         info[0] = matched
         if not matched:
             return []
 
-        return _apply_directives(stream, directives, ctxt, **vars)
+        return _apply_directives(stream, directives, ctxt, vars)
 
 
 class OtherwiseDirective(Directive):
@@ -672,7 +672,7 @@
             return []
         info[0] = True
 
-        return _apply_directives(stream, directives, ctxt, **vars)
+        return _apply_directives(stream, directives, ctxt, vars)
 
 
 class WithDirective(Directive):
@@ -721,10 +721,10 @@
         frame = {}
         ctxt.push(frame)
         for targets, expr in self.vars:
-            value = _eval_expr(expr, ctxt, **vars)
+            value = _eval_expr(expr, ctxt, vars)
             for assign in targets:
                 assign(frame, value)
-        for event in _apply_directives(stream, directives, ctxt, **vars):
+        for event in _apply_directives(stream, directives, ctxt, vars):
             yield event
         ctxt.pop()
 
--- a/genshi/template/markup.py
+++ b/genshi/template/markup.py
@@ -356,7 +356,7 @@
                         pre_end -= 1
                     inner = _strip(stream)
                     if pre_end > 0:
-                        inner = self._match(inner, ctxt, end=pre_end)
+                        inner = self._match(inner, ctxt, end=pre_end, **vars)
                     content = self._include(chain([event], inner, tail), ctxt)
                     if 'not_buffered' not in hints:
                         content = list(content)
@@ -371,7 +371,7 @@
 
                     # Recursively process the output
                     template = _apply_directives(template, directives, ctxt,
-                                                 **vars)
+                                                 vars)
                     for event in self._match(self._flatten(template, ctxt,
                                                            **vars),
                                              ctxt, start=idx + 1, **vars):
Copyright (C) 2012-2017 Edgewall Software