--- a/tornado/web.py
+++ b/tornado/web.py
@@ -1015,7 +1015,7 @@ class Application(object):
RequestHandler._templates = None
RequestHandler._static_hashes = {}
- handler._execute(transforms, *args)
+ handler._execute(transforms, *args, **match.groupdict())
return handler
def reverse_url(self, name, *args):
Any reason not to do it?
-Marc
Oops. I didn't test that very well. It fails when there is no match.
Here's one that works better and is more consistent with the existing
code style:
--- a/tornado/web.py
+++ b/tornado/web.py
@@ -995,6 +995,7 @@ class Application(object):
transforms = [t(request) for t in self.transforms]
handler = None
args = []
+ kwargs = {}
handlers = self._get_host_handlers(request)
if not handlers:
handler = RedirectHandler(
@@ -1005,6 +1006,7 @@ class Application(object):
if match:
handler = spec.handler_class(self, request,
**spec.kwargs)
args = match.groups()
+ kwargs = match.groupdict()
break
if not handler:
handler = ErrorHandler(self, request, 404)
@@ -1015,7 +1017,7 @@ class Application(object):
RequestHandler._templates = None
RequestHandler._static_hashes = {}
- handler._execute(transforms, *args)
+ handler._execute(transforms, *args, **kwargs)
return handler
def reverse_url(self, name, *args):
Hmmm. I should probably learn how to do a git pull request... :-)
>>> m=re.match(r'(?P<foo>foo)(?P<bar>bar)', 'foobar')
>>> m.groups(), m.groupdict()
(('foo', 'bar'), {'foo': 'foo', 'bar': 'bar'})
>>> def f(bar, foo): print bar, foo
...
>>> f(*m.groups(), **m.groupdict())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: f() got multiple values for keyword argument 'foo'
>>> def f2(foo, bar): print foo, bar
...
>>> f2(*m.groups(), **m.groupdict())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: f2() got multiple values for keyword argument 'foo'
-Ben
Too late!
http://groups.google.com/group/python-tornado/browse_thread/thread/1d8a90ad5ec4aa90
Any reason not to do it?
Good point. Well, my handler signature uses (self, *args, **kwargs),
and I just use that to populate a simple dictionary, so it doesn't
matter in my case that the named groups might be included twice in the
function args. That's why I didn't run into that problem.
Here's how Django does it:
http://docs.djangoproject.com/en/dev/topics/http/urls/#s-named-groups
They assume that you are going to use all named groups or all unnamed
groups and not mix them (which seems reasonable). In their words, "If
there are any named arguments, it will use those, ignoring non-named
arguments. Otherwise, it will pass all non-named arguments as
positional arguments.". Following a rule like that, it should be
pretty easy to implement. Something like:
--- a/tornado/web.py
+++ b/tornado/web.py
@@ -995,6 +995,7 @@ class Application(object):
transforms = [t(request) for t in self.transforms]
handler = None
args = []
+ kwargs = {}
handlers = self._get_host_handlers(request)
if not handlers:
handler = RedirectHandler(
@@ -1005,6 +1006,9 @@ class Application(object):
if match:
handler = spec.handler_class(self, request,
**spec.kwargs)
args = match.groups()
+ kwargs = match.groupdict()
+ if kwargs:
+ args = []
break
if not handler:
handler = ErrorHandler(self, request, 404)
@@ -1015,7 +1019,7 @@ class Application(object):
RequestHandler._templates = None
RequestHandler._static_hashes = {}
- handler._execute(transforms, *args)
+ handler._execute(transforms, *args, **kwargs)
return handler
def reverse_url(self, name, *args):
I tested the above with:
class PathRegexHandler(tornado.web.RequestHandler):
def get(self, thing_type, thing_name):
self.write("thing_type = %r\n" % thing_type)
self.write("thing_name = %r\n" % thing_name)
application = tornado.web.Application([
(r'/unnamed_groups/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)',
PathRegexHandler),
(r'/named_groups/(?P<thing_type>[a-zA-Z0-9]+)/(?
P<thing_name>[a-zA-Z0-9]+)', PathRegexHandler),
])
$ curl http://127.0.0.1:8000/unnamed_groups/animal/tiger
thing_type = 'animal'
thing_name = 'tiger'
$ curl http://127.0.0.1:8000/named_groups/animal/tiger
thing_type = 'animal'
thing_name = 'tiger'
How's that?
-Marc
-Ben
what about this?
and
http://groups.google.com/group/python-tornado/browse_thread/thread/5daa559b043cc1cc/7683fdf283ff4fdf
for allow {{{ var }}} in templates?
Ben Darnell schrieb:
Thanks,
-Ben
Cool. Thanks!