# HG changeset patch # User cmlenz # Date 1161016866 0 # Node ID 3425c26d2c090ed7bbb115f84c46c1cd5510f3b6 # Parent 6e6950ac0e56d40a762b2cf20c0670c528cc2070 Minor optimization for XPath evaluation. diff --git a/genshi/path.py b/genshi/path.py --- a/genshi/path.py +++ b/genshi/path.py @@ -129,7 +129,7 @@ elif subevent[0] is END: depth -= 1 yield subevent - test(subevent, namespaces, variables) + test(subevent, namespaces, variables, updateonly=True) elif result: yield result return Stream(_generate()) @@ -141,7 +141,10 @@ The function returned expects the positional arguments `event`, `namespaces` and `variables`. The first is a stream event, while the latter two are a mapping of namespace prefixes to URIs, and a mapping - of variable names to values, respectively. + of variable names to values, respectively. In addition, the function + accepts an `updateonly` keyword argument that default to `False`. If + it is set to `True`, the function only updates its internal state, + but does not perform any tests or return a result. If the path matches the event, the function returns the match (for example, a `START` or `TEXT` event.) Otherwise, it returns `None`. @@ -156,11 +159,10 @@ """ paths = [(p, len(p), [0], [], [0] * len(p)) for p in self.paths] - def _test(event, namespaces, variables): + def _test(event, namespaces, variables, updateonly=False): kind, data, pos = event retval = None for steps, size, cursors, cutoff, counter in paths: - # Manage the stack that tells us "where we are" in the stream if kind is END: if cursors: @@ -169,7 +171,7 @@ elif kind is START: cursors.append(cursors and cursors[-1] or 0) - if retval or not cursors: + if updateonly or retval or not cursors: continue cursor = cursors[-1] depth = len(cursors) diff --git a/genshi/template.py b/genshi/template.py --- a/genshi/template.py +++ b/genshi/template.py @@ -1129,7 +1129,7 @@ # Let the remaining match templates know about the event so # they get a chance to update their internal state for test in [mt[0] for mt in match_templates[idx + 1:]]: - test(event, namespaces, ctxt) + test(event, namespaces, ctxt, updateonly=True) # Consume and store all events until an end event # corresponding to this start event is encountered @@ -1140,7 +1140,7 @@ content = list(content) for test in [mt[0] for mt in match_templates]: - test(tail[0], namespaces, ctxt) + test(tail[0], namespaces, ctxt, updateonly=True) # Make the select() function available in the body of the # match template