Ticket #17492: regex_backreference.diff

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

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

  • 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): 
    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)
    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)))
    149158            elif ch in "*?+{":
    150159                # Quanitifers affect the previous item in the result list.
    151160                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 = ( 
    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..3e906df
    - +  
     1from django.utils import regex_helper
     2from django.utils import unittest
     3
     4class 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 
    2525from .timezone import TimezoneTests
    2626from .crypto import TestUtilsCryptoPBKDF2
    2727from .archive import TestZip, TestTar, TestGzipTar, TestBzip2Tar
     28from .regex_helper import NormalizeTests
Back to Top