From: Ben Darnell Date: Wed, 6 Jul 2011 16:56:46 +0000 (-0700) Subject: Speed up Application.__call__ when there are no matched groups X-Git-Tag: v2.1.0~102 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=18759049e3ca99a8a9b32496f7a80b8430acacae;p=thirdparty%2Ftornado.git Speed up Application.__call__ when there are no matched groups --- diff --git a/tornado/web.py b/tornado/web.py index b656178ea..c5b0ed79d 100644 --- a/tornado/web.py +++ b/tornado/web.py @@ -1305,23 +1305,25 @@ class Application(object): for spec in handlers: match = spec.regex.match(request.path) if match: - # None-safe wrapper around url_unescape to handle - # unmatched optional groups correctly - def unquote(s): - if s is None: return s - return escape.url_unescape(s, encoding=None) handler = spec.handler_class(self, request, **spec.kwargs) - # Pass matched groups to the handler. Since - # match.groups() includes both named and unnamed groups, - # we want to use either groups or groupdict but not both. - # Note that args are passed as bytes so the handler can - # decide what encoding to use. - kwargs = dict((k, unquote(v)) - for (k, v) in match.groupdict().iteritems()) - if kwargs: - args = [] - else: - args = [unquote(s) for s in match.groups()] + if spec.regex.groups: + # None-safe wrapper around url_unescape to handle + # unmatched optional groups correctly + def unquote(s): + if s is None: return s + return escape.url_unescape(s, encoding=None) + # Pass matched groups to the handler. Since + # match.groups() includes both named and unnamed groups, + # we want to use either groups or groupdict but not both. + # Note that args are passed as bytes so the handler can + # decide what encoding to use. + + if spec.regex.groupindex: + kwargs = dict( + (k, unquote(v)) + for (k, v) in match.groupdict().iteritems()) + else: + args = [unquote(s) for s in match.groups()] break if not handler: handler = ErrorHandler(self, request, status_code=404) @@ -1775,6 +1777,8 @@ class URLSpec(object): if not pattern.endswith('$'): pattern += '$' self.regex = re.compile(pattern) + assert len(self.regex.groupindex) in (0, self.regex.groups), \ + "groups in url regexes must either be all named or all positional" self.handler_class = handler_class self.kwargs = kwargs self.name = name