From: Ben Darnell Date: Tue, 20 Jul 2010 17:25:25 +0000 (-0700) Subject: Accept None (i.e. unmatched optional groups) when extracting parameters from X-Git-Tag: v1.0.0~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2c89b89536bbfa081745336bb5ab5465c448cb8a;p=thirdparty%2Ftornado.git Accept None (i.e. unmatched optional groups) when extracting parameters from the path. http://github.com/facebook/tornado/issues/issue/115 --- diff --git a/tornado/web.py b/tornado/web.py index 3beac2390..646fe067b 100644 --- a/tornado/web.py +++ b/tornado/web.py @@ -1060,16 +1060,21 @@ class Application(object): for spec in handlers: match = spec.regex.match(request.path) if match: + # None-safe wrapper around urllib.unquote to handle + # unmatched optional groups correctly + def unquote(s): + if s is None: return s + return urllib.unquote(s) 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. - kwargs = dict((k, urllib.unquote(v)) + kwargs = dict((k, unquote(v)) for (k, v) in match.groupdict().iteritems()) if kwargs: args = [] else: - args = [urllib.unquote(s) for s in match.groups()] + args = [unquote(s) for s in match.groups()] break if not handler: handler = ErrorHandler(self, request, 404)