Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #17492 -- Allow reversal of named backreferences. Thanks nate_b

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17336 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit e52c52ea13be6002ebfa14b7e17ff3004faaf737 1 parent e5719b2
@SmileyChris SmileyChris authored
View
18 django/utils/regex_helper.py
@@ -134,18 +134,28 @@ def normalize(pattern):
raise ValueError("Non-reversible reg-exp portion: '(?%s'" % ch)
else:
ch, escaped = pattern_iter.next()
- if ch != '<':
+ if ch not in ('<', '='):
raise ValueError("Non-reversible reg-exp portion: '(?P%s'" % ch)
# We are in a named capturing group. Extra the name and
# then skip to the end.
+ if ch == '<':
+ terminal_char = '>'
+ # We are in a named backreference.
+ else:
+ terminal_char = ')'
name = []
ch, escaped = pattern_iter.next()
- while ch != '>':
+ while ch != terminal_char:
name.append(ch)
ch, escaped = pattern_iter.next()
param = ''.join(name)
- result.append(Group(((u"%%(%s)s" % param), param)))
- walk_to_end(ch, pattern_iter)
+ # Named backreferences have already consumed the
+ # parenthesis.
+ if terminal_char != ')':
+ result.append(Group(((u"%%(%s)s" % param), param)))
+ walk_to_end(ch, pattern_iter)
+ else:
+ result.append(Group(((u"%%(%s)s" % param), None)))
elif ch in "*?+{":
# Quanitifers affect the previous item in the result list.
count, ch = get_quantifier(ch, pattern_iter)
View
2  tests/regressiontests/urlpatterns_reverse/tests.py
@@ -76,6 +76,8 @@
('people', NoReverseMatch, [], {'name': 'name with spaces'}),
('people2', '/people/name/', [], {}),
('people2a', '/people/name/fred/', ['fred'], {}),
+ ('people_backref', '/people/nate-nate/', ['nate'], {}),
+ ('people_backref', '/people/nate-nate/', [], {'name': 'nate'}),
('optional', '/optional/fred/', [], {'name': 'fred'}),
('optional', '/optional/fred/', ['fred'], {}),
('hardcoded', '/hardcoded/', [], {}),
View
4 tests/regressiontests/urlpatterns_reverse/urls.py
@@ -22,6 +22,7 @@
url(r'^people/(?P<name>\w+)/$', empty_view, name="people"),
url(r'^people/(?:name/)', empty_view, name="people2"),
url(r'^people/(?:name/(\w+)/)?', empty_view, name="people2a"),
+ url(r'^people/(?P<name>\w+)-(?P=name)/$', empty_view, name="people_backref"),
url(r'^optional/(?P<name>.*)/(?:.+/)?', empty_view, name="optional"),
url(r'^hardcoded/$', empty_view, name="hardcoded"),
url(r'^hardcoded/doc\.pdf$', empty_view, name="hardcoded2"),
@@ -65,7 +66,4 @@
(r'defaults_view2/(?P<arg1>\d+)/', 'defaults_view', {'arg2': 2}, 'defaults'),
url('^includes/', include(other_patterns)),
-
)
-
-
View
48 tests/regressiontests/utils/regex_helper.py
@@ -0,0 +1,48 @@
+from django.utils import regex_helper
+from django.utils import unittest
+
+
+class NormalizeTests(unittest.TestCase):
+ def test_empty(self):
+ pattern = r""
+ expected = [(u'', [])]
+ result = regex_helper.normalize(pattern)
+ self.assertEqual(result, expected)
+
+ def test_escape(self):
+ pattern = r"\\\^\$\.\|\?\*\+\(\)\["
+ expected = [(u'\\^$.|?*+()[', [])]
+ result = regex_helper.normalize(pattern)
+ self.assertEqual(result, expected)
+
+ def test_group_positional(self):
+ pattern = r"(.*)-(.+)"
+ expected = [(u'%(_0)s-%(_1)s', ['_0', '_1'])]
+ result = regex_helper.normalize(pattern)
+ self.assertEqual(result, expected)
+
+ def test_group_ignored(self):
+ pattern = r"(?i)(?L)(?m)(?s)(?u)(?#)"
+ expected = [(u'', [])]
+ result = regex_helper.normalize(pattern)
+ self.assertEqual(result, expected)
+
+ def test_group_noncapturing(self):
+ pattern = r"(?:non-capturing)"
+ expected = [(u'non-capturing', [])]
+ result = regex_helper.normalize(pattern)
+ self.assertEqual(result, expected)
+
+ def test_group_named(self):
+ pattern = r"(?P<first_group_name>.*)-(?P<second_group_name>.*)"
+ expected = [(u'%(first_group_name)s-%(second_group_name)s',
+ ['first_group_name', 'second_group_name'])]
+ result = regex_helper.normalize(pattern)
+ self.assertEqual(result, expected)
+
+ def test_group_backreference(self):
+ pattern = r"(?P<first_group_name>.*)-(?P=first_group_name)"
+ expected = [(u'%(first_group_name)s-%(first_group_name)s',
+ ['first_group_name'])]
+ result = regex_helper.normalize(pattern)
+ self.assertEqual(result, expected)
View
1  tests/regressiontests/utils/tests.py
@@ -25,3 +25,4 @@
from .timezone import TimezoneTests
from .crypto import TestUtilsCryptoPBKDF2
from .archive import TestZip, TestTar, TestGzipTar, TestBzip2Tar
+from .regex_helper import NormalizeTests
Please sign in to comment.
Something went wrong with that request. Please try again.