Ticket #17492: regex_backreference.diff
File regex_backreference.diff, 6.7 KB (added by , 13 years ago) |
---|
-
django/utils/regex_helper.py
From 24619fa7c3e05f96ec80f6d33dccaa4ad0a0f716 Mon Sep 17 00:00:00 2001 From: Nate Bragg <jonathan.bragg@alum.rpi.edu> Date: Tue, 3 Jan 2012 17:07:09 -0500 Subject: [PATCH] Changes for backreference reversal --- django/utils/regex_helper.py | 17 +++++-- tests/regressiontests/urlpatterns_reverse/tests.py | 2 + tests/regressiontests/urlpatterns_reverse/urls.py | 4 +- tests/regressiontests/utils/regex_helper.py | 47 ++++++++++++++++++++ tests/regressiontests/utils/tests.py | 1 + 5 files changed, 64 insertions(+), 7 deletions(-) create mode 100644 tests/regressiontests/utils/regex_helper.py diff --git a/django/utils/regex_helper.py b/django/utils/regex_helper.py index b11fe96..8137657 100644
a b def normalize(pattern): 134 134 raise ValueError("Non-reversible reg-exp portion: '(?%s'" % ch) 135 135 else: 136 136 ch, escaped = pattern_iter.next() 137 if ch != '<':137 if ch not in ('<', '='): 138 138 raise ValueError("Non-reversible reg-exp portion: '(?P%s'" % ch) 139 139 # We are in a named capturing group. Extra the name and 140 140 # then skip to the end. 141 if ch == '<': 142 terminal_char = '>' 143 # We are in a named backreference. 144 else: 145 terminal_char = ')' 141 146 name = [] 142 147 ch, escaped = pattern_iter.next() 143 while ch != '>':148 while ch != terminal_char: 144 149 name.append(ch) 145 150 ch, escaped = pattern_iter.next() 146 151 param = ''.join(name) 147 result.append(Group(((u"%%(%s)s" % param), param))) 148 walk_to_end(ch, pattern_iter) 152 #named backreferences have already consumed the parens 153 if terminal_char != ')': 154 result.append(Group(((u"%%(%s)s" % param), param))) 155 walk_to_end(ch, pattern_iter) 156 else: 157 result.append(Group(((u"%%(%s)s" % param), None))) 149 158 elif ch in "*?+{": 150 159 # Quanitifers affect the previous item in the result list. 151 160 count, ch = get_quantifier(ch, pattern_iter) -
tests/regressiontests/urlpatterns_reverse/tests.py
diff --git a/tests/regressiontests/urlpatterns_reverse/tests.py b/tests/regressiontests/urlpatterns_reverse/tests.py index a5df26f..a1c9244 100644
a b test_data = ( 76 76 ('people', NoReverseMatch, [], {'name': 'name with spaces'}), 77 77 ('people2', '/people/name/', [], {}), 78 78 ('people2a', '/people/name/fred/', ['fred'], {}), 79 ('people_backref', '/people/nate-nate/', ['nate'], {}), 80 ('people_backref', '/people/nate-nate/', [], {'name': 'nate'}), 79 81 ('optional', '/optional/fred/', [], {'name': 'fred'}), 80 82 ('optional', '/optional/fred/', ['fred'], {}), 81 83 ('hardcoded', '/hardcoded/', [], {}), -
tests/regressiontests/urlpatterns_reverse/urls.py
diff --git a/tests/regressiontests/urlpatterns_reverse/urls.py b/tests/regressiontests/urlpatterns_reverse/urls.py index 5bde2b0..1d4ae73 100644
a b urlpatterns = patterns('', 22 22 url(r'^people/(?P<name>\w+)/$', empty_view, name="people"), 23 23 url(r'^people/(?:name/)', empty_view, name="people2"), 24 24 url(r'^people/(?:name/(\w+)/)?', empty_view, name="people2a"), 25 url(r'^people/(?P<name>\w+)-(?P=name)/$', empty_view, name="people_backref"), 25 26 url(r'^optional/(?P<name>.*)/(?:.+/)?', empty_view, name="optional"), 26 27 url(r'^hardcoded/$', empty_view, name="hardcoded"), 27 28 url(r'^hardcoded/doc\.pdf$', empty_view, name="hardcoded2"), … … urlpatterns = patterns('', 65 66 (r'defaults_view2/(?P<arg1>\d+)/', 'defaults_view', {'arg2': 2}, 'defaults'), 66 67 67 68 url('^includes/', include(other_patterns)), 68 69 69 ) 70 71 -
new file tests/regressiontests/utils/regex_helper.py
diff --git a/tests/regressiontests/utils/regex_helper.py b/tests/regressiontests/utils/regex_helper.py new file mode 100644 index 0000000..3e906df
- + 1 from django.utils import regex_helper 2 from django.utils import unittest 3 4 class NormalizeTests(unittest.TestCase): 5 def test_empty(self): 6 pattern = r"" 7 expected = [(u'', [])] 8 result = regex_helper.normalize(pattern) 9 self.assertEqual(result,expected) 10 11 def test_escape(self): 12 pattern = r"\\\^\$\.\|\?\*\+\(\)\[" 13 expected = [(ur'\^$.|?*+()[', [])] 14 result = regex_helper.normalize(pattern) 15 self.assertEqual(result,expected) 16 17 def test_group_positional(self): 18 pattern = r"(.*)-(.+)" 19 expected = [(u'%(_0)s-%(_1)s', ['_0', '_1'])] 20 result = regex_helper.normalize(pattern) 21 self.assertEqual(result,expected) 22 23 def test_group_ignored(self): 24 pattern = r"(?i)(?L)(?m)(?s)(?u)(?#)" 25 expected = [(u'', [])] 26 result = regex_helper.normalize(pattern) 27 self.assertEqual(result,expected) 28 29 def test_group_noncapturing(self): 30 pattern = r"(?:non-capturing)" 31 expected = [(u'non-capturing', [])] 32 result = regex_helper.normalize(pattern) 33 self.assertEqual(result,expected) 34 35 def test_group_named(self): 36 pattern = r"(?P<first_group_name>.*)-(?P<second_group_name>.*)" 37 expected = [(u'%(first_group_name)s-%(second_group_name)s', 38 ['first_group_name', 'second_group_name'])] 39 result = regex_helper.normalize(pattern) 40 self.assertEqual(result,expected) 41 42 def test_group_backreference(self): 43 pattern = r"(?P<first_group_name>.*)-(?P=first_group_name)" 44 expected = [(u'%(first_group_name)s-%(first_group_name)s', 45 ['first_group_name', 'first_group_name'])] 46 result = regex_helper.normalize(pattern) 47 self.assertEqual(result,expected) -
tests/regressiontests/utils/tests.py
diff --git a/tests/regressiontests/utils/tests.py b/tests/regressiontests/utils/tests.py index f48a4ad..f5ca06e 100644
a b from .ipv6 import TestUtilsIPv6 25 25 from .timezone import TimezoneTests 26 26 from .crypto import TestUtilsCryptoPBKDF2 27 27 from .archive import TestZip, TestTar, TestGzipTar, TestBzip2Tar 28 from .regex_helper import NormalizeTests