Ticket #17492: 17492.diff

File 17492.diff, 5.9 KB (added by Chris Beaven, 13 years ago)
  • django/utils/regex_helper.py

    diff --git a/django/utils/regex_helper.py b/django/utils/regex_helper.py
    index b11fe96..6ca92f5 100644
    a b def normalize(pattern):  
    134134                        raise ValueError("Non-reversible reg-exp portion: '(?%s'" % ch)
    135135                    else:
    136136                        ch, escaped = pattern_iter.next()
    137                         if ch != '<':
     137                        if ch not in ('<', '='):
    138138                            raise ValueError("Non-reversible reg-exp portion: '(?P%s'" % ch)
    139139                        # We are in a named capturing group. Extra the name and
    140140                        # then skip to the end.
     141                        if ch == '<':
     142                            terminal_char = '>'
     143                        # We are in a named backreference.
     144                        else:
     145                            terminal_char = ')'
    141146                        name = []
    142147                        ch, escaped = pattern_iter.next()
    143                         while ch != '>':
     148                        while ch != terminal_char:
    144149                            name.append(ch)
    145150                            ch, escaped = pattern_iter.next()
    146151                        param = ''.join(name)
    147152                        result.append(Group(((u"%%(%s)s" % param), param)))
    148                         walk_to_end(ch, pattern_iter)
     153                        # Named backreferences have already consumed the
     154                        # parenthesis.
     155                        if terminal_char != ')':
     156                            walk_to_end(ch, pattern_iter)
    149157            elif ch in "*?+{":
    150158                # Quanitifers affect the previous item in the result list.
    151159                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..7219c1b 100644
    a b test_data = (  
    7676    ('people', NoReverseMatch, [], {'name': 'name with spaces'}),
    7777    ('people2', '/people/name/', [], {}),
    7878    ('people2a', '/people/name/fred/', ['fred'], {}),
     79#    ('people_backref', '/people/nate-nate/', ['nate'], {}),
     80    ('people_backref', '/people/nate-nate/', [], {'name': 'nate'}),
    7981    ('optional', '/optional/fred/', [], {'name': 'fred'}),
    8082    ('optional', '/optional/fred/', ['fred'], {}),
    8183    ('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('',  
    2222    url(r'^people/(?P<name>\w+)/$', empty_view, name="people"),
    2323    url(r'^people/(?:name/)', empty_view, name="people2"),
    2424    url(r'^people/(?:name/(\w+)/)?', empty_view, name="people2a"),
     25    url(r'^people/(?P<name>\w+)-(?P=name)/$', empty_view, name="people_backref"),
    2526    url(r'^optional/(?P<name>.*)/(?:.+/)?', empty_view, name="optional"),
    2627    url(r'^hardcoded/$', empty_view, name="hardcoded"),
    2728    url(r'^hardcoded/doc\.pdf$', empty_view, name="hardcoded2"),
    urlpatterns = patterns('',  
    6566    (r'defaults_view2/(?P<arg1>\d+)/', 'defaults_view', {'arg2': 2}, 'defaults'),
    6667
    6768    url('^includes/', include(other_patterns)),
    68 
    6969)
    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..8dc712f
    - +  
     1from django.utils import regex_helper
     2from django.utils import unittest
     3
     4
     5class NormalizeTests(unittest.TestCase):
     6    def test_empty(self):
     7        pattern = r""
     8        expected = [(u'', [])]
     9        result = regex_helper.normalize(pattern)
     10        self.assertEqual(result, expected)
     11
     12    def test_escape(self):
     13        pattern = r"\\\^\$\.\|\?\*\+\(\)\["
     14        expected = [(u'\\^$.|?*+()[', [])]
     15        result = regex_helper.normalize(pattern)
     16        self.assertEqual(result, expected)
     17
     18    def test_group_positional(self):
     19        pattern = r"(.*)-(.+)"
     20        expected = [(u'%(_0)s-%(_1)s', ['_0', '_1'])]
     21        result = regex_helper.normalize(pattern)
     22        self.assertEqual(result, expected)
     23
     24    def test_group_ignored(self):
     25        pattern = r"(?i)(?L)(?m)(?s)(?u)(?#)"
     26        expected = [(u'', [])]
     27        result = regex_helper.normalize(pattern)
     28        self.assertEqual(result, expected)
     29
     30    def test_group_noncapturing(self):
     31        pattern = r"(?:non-capturing)"
     32        expected = [(u'non-capturing', [])]
     33        result = regex_helper.normalize(pattern)
     34        self.assertEqual(result, expected)
     35
     36    def test_group_named(self):
     37        pattern = r"(?P<first_group_name>.*)-(?P<second_group_name>.*)"
     38        expected = [(u'%(first_group_name)s-%(second_group_name)s',
     39                    ['first_group_name', 'second_group_name'])]
     40        result = regex_helper.normalize(pattern)
     41        self.assertEqual(result, expected)
     42
     43    def test_group_backreference(self):
     44        pattern = r"(?P<first_group_name>.*)-(?P=first_group_name)"
     45        expected = [(u'%(first_group_name)s-%(first_group_name)s',
     46                    ['first_group_name'])]
     47        result = regex_helper.normalize(pattern)
     48        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  
    2525from .timezone import TimezoneTests
    2626from .crypto import TestUtilsCryptoPBKDF2
    2727from .archive import TestZip, TestTar, TestGzipTar, TestBzip2Tar
     28from .regex_helper import NormalizeTests
Back to Top