From 2c89b89536bbfa081745336bb5ab5465c448cb8a Mon Sep 17 00:00:00 2001 From: Ben Darnell Date: Tue, 20 Jul 2010 10:25:25 -0700 Subject: [PATCH] Accept None (i.e. unmatched optional groups) when extracting parameters from the path. http://github.com/facebook/tornado/issues/issue/115 --- tornado/web.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) 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) -- 2.47.2