diff options
| author | Chris McDonough <chrism@plope.com> | 2011-04-19 15:03:39 -0400 |
|---|---|---|
| committer | Chris McDonough <chrism@plope.com> | 2011-04-19 15:03:39 -0400 |
| commit | bea9f5b1b99fc27ee2a79512d098d688aea9d113 (patch) | |
| tree | 989d8d0aab37d6ee104cbd6baa99bbf363550115 | |
| parent | db51c08cda155ae5d9921c62700ab813f58f5d72 (diff) | |
| parent | f85522aaf475aa6d550ab34f0fe5ab9b018b37fe (diff) | |
| download | pyramid-bea9f5b1b99fc27ee2a79512d098d688aea9d113.tar.gz pyramid-bea9f5b1b99fc27ee2a79512d098d688aea9d113.tar.bz2 pyramid-bea9f5b1b99fc27ee2a79512d098d688aea9d113.zip | |
Merge branch '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 092423bc1..5f08606be 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -76,6 +76,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) |
