diff options
| author | Chris McDonough <chrism@plope.com> | 2011-04-19 15:01:26 -0400 |
|---|---|---|
| committer | Chris McDonough <chrism@plope.com> | 2011-04-19 15:01:26 -0400 |
| commit | f85522aaf475aa6d550ab34f0fe5ab9b018b37fe (patch) | |
| tree | a25fd0dbaa547502caf63e09b9f8e38c923f8166 | |
| parent | d3d17a21236ab0f3f1de52bd6bbde3eb3f045643 (diff) | |
| parent | e099a08f680c96afb931842ed15003626611dd7a (diff) | |
| download | pyramid-f85522aaf475aa6d550ab34f0fe5ab9b018b37fe.tar.gz pyramid-f85522aaf475aa6d550ab34f0fe5ab9b018b37fe.tar.bz2 pyramid-f85522aaf475aa6d550ab34f0fe5ab9b018b37fe.zip | |
Merge branch 'master' of https://github.com/jgonera/pyramid into jgonera-master
| -rw-r--r-- | CHANGES.txt | 10 | ||||
| -rw-r--r-- | pyramid/tests/test_urldispatch.py | 36 | ||||
| -rw-r--r-- | pyramid/urldispatch.py | 19 |
3 files changed, 37 insertions, 28 deletions
diff --git a/CHANGES.txt b/CHANGES.txt index affacb7d4..95387b03b 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -72,6 +72,16 @@ Features Bug Fixes --------- +- URL pattern markers used in URL dispatch are permitted to specify a custom + regex. For example, the pattern ``/{foo:\d+}`` means to match ``/12345`` + (foo==12345 in the match dictionary) but not ``/abc``. However, custom + regexes in a pattern marker which used squiggly brackets did not work. For + example, ``/{foo:\d{4}}`` would fail to match ``/1234`` and + ``/{foo:\d{1,2}}`` would fail to match ``/1`` or ``/11``. One level of + inner squiggly brackets is now recognized so that the prior two patterns + given as examples now work. See also + https://github.com/Pylons/pyramid/issues/#issue/123. + - Don't send port numbers along with domain information in cookies set by AuthTktCookieHelper (see https://github.com/Pylons/pyramid/issues/131). diff --git a/pyramid/tests/test_urldispatch.py b/pyramid/tests/test_urldispatch.py index 265a5bb0a..6e1474b1d 100644 --- a/pyramid/tests/test_urldispatch.py +++ b/pyramid/tests/test_urldispatch.py @@ -288,20 +288,28 @@ class TestCompileRoute(unittest.TestCase): {'baz':'abc', 'buz':'buz'}) self.assertEqual(generator({'baz':1, 'buz':2}), '/foo/1/biz/2/bar') - # XXX reenable after torturous_route_re replacement is found for - # Jython - ## def test_custom_regex_with_embedded_squigglies(self): - ## matcher, generator = self._callFUT('/{buz:\d{4}}') - ## self.assertEqual(matcher('/2001'), {'buz':'2001'}) - ## self.assertEqual(matcher('/200'), None) - ## self.assertEqual(generator({'buz':2001}), '/2001') - - ## def test_custom_regex_with_embedded_squigglies2(self): - ## matcher, generator = self._callFUT('/{buz:\d{3,4}}') - ## self.assertEqual(matcher('/2001'), {'buz':'2001'}) - ## self.assertEqual(matcher('/200'), {'buz':'200'}) - ## self.assertEqual(matcher('/20'), None) - ## self.assertEqual(generator({'buz':2001}), '/2001') + def test_custom_regex_with_embedded_squigglies(self): + matcher, generator = self._callFUT('/{buz:\d{4}}') + self.assertEqual(matcher('/2001'), {'buz':'2001'}) + self.assertEqual(matcher('/200'), None) + self.assertEqual(generator({'buz':2001}), '/2001') + + def test_custom_regex_with_embedded_squigglies2(self): + matcher, generator = self._callFUT('/{buz:\d{3,4}}') + self.assertEqual(matcher('/2001'), {'buz':'2001'}) + self.assertEqual(matcher('/200'), {'buz':'200'}) + self.assertEqual(matcher('/20'), None) + self.assertEqual(generator({'buz':2001}), '/2001') + + def test_custom_regex_with_embedded_squigglies3(self): + matcher, generator = self._callFUT('/{buz:(\d{2}|\d{4})-[a-zA-Z]{3,4}-\d{2}}') + self.assertEqual(matcher('/2001-Nov-15'), {'buz':'2001-Nov-15'}) + self.assertEqual(matcher('/99-June-10'), {'buz':'99-June-10'}) + self.assertEqual(matcher('/2-Nov-15'), None) + self.assertEqual(matcher('/200-Nov-15'), None) + self.assertEqual(matcher('/2001-No-15'), None) + self.assertEqual(generator({'buz':'2001-Nov-15'}), '/2001-Nov-15') + self.assertEqual(generator({'buz':'99-June-10'}), '/99-June-10') class TestCompileRouteMatchFunctional(unittest.TestCase): def matches(self, pattern, path, expected): diff --git a/pyramid/urldispatch.py b/pyramid/urldispatch.py index be737201b..989cc62b9 100644 --- a/pyramid/urldispatch.py +++ b/pyramid/urldispatch.py @@ -77,20 +77,11 @@ class RoutesMapper(object): old_route_re = re.compile(r'(\:[a-zA-Z]\w*)') star_in_brackets = re.compile(r'\{[^\}]*\*\w*[^\}]*\}') -# The regex named ``torturous_route_re`` below allows us to support at least -# one level of "inner" squigglies inside the expr of a {name:expr} pattern; -# for example, {foo:\d{4}}. Thanks to Zart for the regex help. -# ``torturous_route_re`` is meant to be a replacement for the regex named -# ``route_re`` (which is just (\{[a-zA-Z][^\}]*\})) because ``route_re`` -# chokes when it encounters inner squigglies. However -# http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5050507 means that the -# torturous regex doesn't work on Jython (recursion error), so we've disabled -# it in favor of ``route_re`` for now. If somebody can make something that -# will work on Jython but also match inner squigglies, it'd be useful. - -# torturous_route_re = re.compile(r'(\{[a-zA-Z](?:\{[^\}]*\}|[^\{\}]*)*\})') - -route_re = re.compile(r'(\{[a-zA-Z][^\}]*\})') +# The torturous nature of the regex named ``route_re`` below is due to the +# fact that we need to support at least one level of "inner" squigglies +# inside the expr of a {name:expr} pattern. This regex used to be just +# (\{[a-zA-Z][^\}]*\}) but that choked when supplied with e.g. {foo:\d{4}}. +route_re = re.compile(r'(\{[a-zA-Z][^{}]*(?:\{[^{}]*\}[^{}]*)*\})') def update_pattern(matchobj): name = matchobj.group(0) |
