]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Speed up Application.__call__ when there are no matched groups
authorBen Darnell <ben@bendarnell.com>
Wed, 6 Jul 2011 16:56:46 +0000 (09:56 -0700)
committerBen Darnell <ben@bendarnell.com>
Thu, 7 Jul 2011 02:15:49 +0000 (19:15 -0700)
tornado/web.py

index b656178ea4d09c21927d4e9cfdc0a537a4f4b3d1..c5b0ed79d7760e2c072c28f5bb9e8afc2250a9cd 100644 (file)
@@ -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