Skip to content

Commit

Permalink
Fix issue #264 with error during constructing of url with regex parts
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Jan 29, 2015
1 parent ee35cfe commit 947c1a2
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.14.3
current_version = 0.14.4
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)((?P<release>[a-z]+\d+))?
serialize =
{major}.{minor}.{patch}{release}
Expand Down
5 changes: 5 additions & 0 deletions CHANGES.txt
@@ -1,6 +1,11 @@
CHANGES
=======

0.14.4 (01-29-2015)
-------------------

- Fix issue with error during constructing of url with regex parts #264

0.14.3 (01-28-2015)
-------------------

Expand Down
2 changes: 1 addition & 1 deletion aiohttp/__init__.py
@@ -1,6 +1,6 @@
# This relies on each of the submodules having an __all__ variable.

__version__ = '0.14.3'
__version__ = '0.14.4'


from . import hdrs # noqa
Expand Down
7 changes: 6 additions & 1 deletion aiohttp/web.py
Expand Up @@ -1401,6 +1401,7 @@ def add_route(self, method, path, handler, *, name=None):
assert method in self.METHODS, method
parts = []
factory = PlainRoute
format_parts = []
for part in path.split('/'):
if not part:
continue
Expand All @@ -1409,16 +1410,19 @@ def add_route(self, method, path, handler, *, name=None):
parts.append('(?P<' + match.group('var') + '>' +
self.GOOD + ')')
factory = DynamicRoute
format_parts.append('{'+match.group('var')+'}')
continue

match = self.DYN_WITH_RE.match(part)
if match:
parts.append('(?P<' + match.group('var') + '>' +
match.group('re') + ')')
factory = DynamicRoute
format_parts.append('{'+match.group('var')+'}')
continue
if self.PLAIN.match(part):
parts.append(re.escape(part))
format_parts.append(part)
continue
raise ValueError("Invalid path '{}'['{}']".format(path, part))
if factory is PlainRoute:
Expand All @@ -1432,7 +1436,8 @@ def add_route(self, method, path, handler, *, name=None):
except re.error as exc:
raise ValueError(
"Bad pattern '{}': {}".format(pattern, exc)) from None
route = DynamicRoute(method, handler, name, compiled, path)
formatter = '/' + '/'.join(format_parts)
route = DynamicRoute(method, handler, name, compiled, formatter)
self._register_endpoint(route)
return route

Expand Down
8 changes: 8 additions & 0 deletions tests/test_urldispatch.py
Expand Up @@ -326,3 +326,11 @@ def test_add_route_with_invalid_re(self):
"Bad pattern '/handler/(?P<to>+++)': nothing to repeat",
str(ctx.exception))
self.assertIsNone(ctx.exception.__cause__)

def test_route_dynamic_with_regex_spec(self):
handler = self.make_handler()
route = self.router.add_route('GET', '/get/{num:^\d+}', handler,
name='name')

url = route.url(parts={'num': '123'})
self.assertEqual('/get/123', url)

0 comments on commit 947c1a2

Please sign in to comment.