GoogleCloudPlatform / webapp2

webapp2 is a framework for Google App Engine
https://webapp2.readthedocs.org
Other
141 stars 63 forks source link

Docs for webapp2.Router.__init__() and add() misleading #135

Open vrobertbaruch opened 6 years ago

vrobertbaruch commented 6 years ago

The docs for webapp2.Router.__init__() and add() are misleading:

Parameters: | routes – A sequence of Route instances or, for simple routes, tuples (regex, handler).

In fact, a simple route tuple is of the form (template, handler). It does not take a regex, as in something returned by re.compile().

The documentation for simple route is also misleading:

The simplest form of URI route in webapp2 is a tuple (regex, handler), where regex is a regular expression to match the requested URI path and handler is a callable to handle the request.

This is not true. regex is, in fact, a template, not a Python regex.

This is what happens if you try to use a Python regex as the regex part of the tuple:

Traceback (most recent call last):
  File "/base/alloc/tmpfs/dynamic_runtimes/python27/277b61042b697c7a_unzipped/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 267, in Handle
    result = handler(dict(self._environ), self._StartResponse)
  File "/base/alloc/tmpfs/dynamic_runtimes/python27/277b61042b697c7a_unzipped/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1519, in __call__
    response = self._internal_error(e)
  File "/base/alloc/tmpfs/dynamic_runtimes/python27/277b61042b697c7a_unzipped/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1511, in __call__
    rv = self.handle_exception(request, response, e)
  File "/base/alloc/tmpfs/dynamic_runtimes/python27/277b61042b697c7a_unzipped/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1505, in __call__
    rv = self.router.dispatch(request, response)
  File "/base/alloc/tmpfs/dynamic_runtimes/python27/277b61042b697c7a_unzipped/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1240, in default_dispatcher
    route, args, kwargs = rv = self.match(request)
  File "/base/alloc/tmpfs/dynamic_runtimes/python27/277b61042b697c7a_unzipped/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1180, in default_matcher
    match = route.match(request)
  File "/base/alloc/tmpfs/dynamic_runtimes/python27/277b61042b697c7a_unzipped/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 825, in match
    match = self.regex.match(urllib.unquote(request.path))
  File "/base/alloc/tmpfs/dynamic_runtimes/python27/277b61042b697c7a_unzipped/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 701, in __get__
    value = self.func(obj)
  File "/base/alloc/tmpfs/dynamic_runtimes/python27/277b61042b697c7a_unzipped/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 812, in regex
    if not self.template.startswith('^'):
AttributeError: '_sre.SRE_Pattern' object has no attribute 'startswith'
theacodes commented 6 years ago

Thanks for filing @vrobertbaruch! This project is completely community supported, so if you want to see this fixed please send a pull request!