Code

Ticket #17492: regex_backreference.diff

File regex_backreference.diff, 6.7 KB (added by nate_b, 2 years ago)

A patch to regex_helper.py, along with unit tests for normalize.

Line 
1From 24619fa7c3e05f96ec80f6d33dccaa4ad0a0f716 Mon Sep 17 00:00:00 2001
2From: Nate Bragg <jonathan.bragg@alum.rpi.edu>
3Date: Tue, 3 Jan 2012 17:07:09 -0500
4Subject: [PATCH] Changes for backreference reversal
5
6---
7 django/utils/regex_helper.py                       |   17 +++++--
8 tests/regressiontests/urlpatterns_reverse/tests.py |    2 +
9 tests/regressiontests/urlpatterns_reverse/urls.py  |    4 +-
10 tests/regressiontests/utils/regex_helper.py        |   47 ++++++++++++++++++++
11 tests/regressiontests/utils/tests.py               |    1 +
12 5 files changed, 64 insertions(+), 7 deletions(-)
13 create mode 100644 tests/regressiontests/utils/regex_helper.py
14
15diff --git a/django/utils/regex_helper.py b/django/utils/regex_helper.py
16index b11fe96..8137657 100644
17--- a/django/utils/regex_helper.py
18+++ b/django/utils/regex_helper.py
19@@ -134,18 +134,27 @@ def normalize(pattern):
20                         raise ValueError("Non-reversible reg-exp portion: '(?%s'" % ch)
21                     else:
22                         ch, escaped = pattern_iter.next()
23-                        if ch != '<':
24+                        if ch not in ('<', '='):
25                             raise ValueError("Non-reversible reg-exp portion: '(?P%s'" % ch)
26                         # We are in a named capturing group. Extra the name and
27                         # then skip to the end.
28+                        if ch == '<':
29+                            terminal_char = '>'
30+                        # We are in a named backreference.
31+                        else:
32+                            terminal_char = ')'
33                         name = []
34                         ch, escaped = pattern_iter.next()
35-                        while ch != '>':
36+                        while ch != terminal_char:
37                             name.append(ch)
38                             ch, escaped = pattern_iter.next()
39                         param = ''.join(name)
40-                        result.append(Group(((u"%%(%s)s" % param), param)))
41-                        walk_to_end(ch, pattern_iter)
42+                        #named backreferences have already consumed the parens
43+                        if terminal_char != ')':
44+                            result.append(Group(((u"%%(%s)s" % param), param)))
45+                            walk_to_end(ch, pattern_iter)
46+                        else:
47+                            result.append(Group(((u"%%(%s)s" % param), None)))
48             elif ch in "*?+{":
49                 # Quanitifers affect the previous item in the result list.
50                 count, ch = get_quantifier(ch, pattern_iter)
51diff --git a/tests/regressiontests/urlpatterns_reverse/tests.py b/tests/regressiontests/urlpatterns_reverse/tests.py
52index a5df26f..a1c9244 100644
53--- a/tests/regressiontests/urlpatterns_reverse/tests.py
54+++ b/tests/regressiontests/urlpatterns_reverse/tests.py
55@@ -76,6 +76,8 @@ test_data = (
56     ('people', NoReverseMatch, [], {'name': 'name with spaces'}),
57     ('people2', '/people/name/', [], {}),
58     ('people2a', '/people/name/fred/', ['fred'], {}),
59+    ('people_backref', '/people/nate-nate/', ['nate'], {}),
60+    ('people_backref', '/people/nate-nate/', [], {'name': 'nate'}),
61     ('optional', '/optional/fred/', [], {'name': 'fred'}),
62     ('optional', '/optional/fred/', ['fred'], {}),
63     ('hardcoded', '/hardcoded/', [], {}),
64diff --git a/tests/regressiontests/urlpatterns_reverse/urls.py b/tests/regressiontests/urlpatterns_reverse/urls.py
65index 5bde2b0..1d4ae73 100644
66--- a/tests/regressiontests/urlpatterns_reverse/urls.py
67+++ b/tests/regressiontests/urlpatterns_reverse/urls.py
68@@ -22,6 +22,7 @@ urlpatterns = patterns('',
69     url(r'^people/(?P<name>\w+)/$', empty_view, name="people"),
70     url(r'^people/(?:name/)', empty_view, name="people2"),
71     url(r'^people/(?:name/(\w+)/)?', empty_view, name="people2a"),
72+    url(r'^people/(?P<name>\w+)-(?P=name)/$', empty_view, name="people_backref"),
73     url(r'^optional/(?P<name>.*)/(?:.+/)?', empty_view, name="optional"),
74     url(r'^hardcoded/$', empty_view, name="hardcoded"),
75     url(r'^hardcoded/doc\.pdf$', empty_view, name="hardcoded2"),
76@@ -65,7 +66,4 @@ urlpatterns = patterns('',
77     (r'defaults_view2/(?P<arg1>\d+)/', 'defaults_view', {'arg2': 2}, 'defaults'),
78 
79     url('^includes/', include(other_patterns)),
80-
81 )
82-
83-
84diff --git a/tests/regressiontests/utils/regex_helper.py b/tests/regressiontests/utils/regex_helper.py
85new file mode 100644
86index 0000000..3e906df
87--- /dev/null
88+++ b/tests/regressiontests/utils/regex_helper.py
89@@ -0,0 +1,47 @@
90+from django.utils import regex_helper
91+from django.utils import unittest
92+
93+class NormalizeTests(unittest.TestCase):
94+    def test_empty(self):
95+        pattern = r""
96+        expected = [(u'', [])]
97+        result = regex_helper.normalize(pattern)
98+        self.assertEqual(result,expected)
99+
100+    def test_escape(self):
101+        pattern = r"\\\^\$\.\|\?\*\+\(\)\["
102+        expected = [(ur'\^$.|?*+()[', [])]
103+        result = regex_helper.normalize(pattern)
104+        self.assertEqual(result,expected)
105+
106+    def test_group_positional(self):
107+        pattern = r"(.*)-(.+)"
108+        expected = [(u'%(_0)s-%(_1)s', ['_0', '_1'])]
109+        result = regex_helper.normalize(pattern)
110+        self.assertEqual(result,expected)
111+
112+    def test_group_ignored(self):
113+        pattern = r"(?i)(?L)(?m)(?s)(?u)(?#)"
114+        expected = [(u'', [])]
115+        result = regex_helper.normalize(pattern)
116+        self.assertEqual(result,expected)
117+
118+    def test_group_noncapturing(self):
119+        pattern = r"(?:non-capturing)"
120+        expected = [(u'non-capturing', [])]
121+        result = regex_helper.normalize(pattern)
122+        self.assertEqual(result,expected)
123+
124+    def test_group_named(self):
125+        pattern = r"(?P<first_group_name>.*)-(?P<second_group_name>.*)"
126+        expected = [(u'%(first_group_name)s-%(second_group_name)s',
127+                    ['first_group_name', 'second_group_name'])]
128+        result = regex_helper.normalize(pattern)
129+        self.assertEqual(result,expected)
130+
131+    def test_group_backreference(self):
132+        pattern = r"(?P<first_group_name>.*)-(?P=first_group_name)"
133+        expected = [(u'%(first_group_name)s-%(first_group_name)s',
134+                    ['first_group_name', 'first_group_name'])]
135+        result = regex_helper.normalize(pattern)
136+        self.assertEqual(result,expected)
137diff --git a/tests/regressiontests/utils/tests.py b/tests/regressiontests/utils/tests.py
138index f48a4ad..f5ca06e 100644
139--- a/tests/regressiontests/utils/tests.py
140+++ b/tests/regressiontests/utils/tests.py
141@@ -25,3 +25,4 @@ from .ipv6 import TestUtilsIPv6
142 from .timezone import TimezoneTests
143 from .crypto import TestUtilsCryptoPBKDF2
144 from .archive import TestZip, TestTar, TestGzipTar, TestBzip2Tar
145+from .regex_helper import NormalizeTests
146--
1471.7.5.4
148