Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #6170 -- Ensured that a useful exception is raised when a regex…

… is invalid in the URLConf.

Thanks to abrahamson.j for the report, to guettli for initial work on the patch, and to David Gouldin for the new patch and test.
  • Loading branch information...
commit 1af0271d7c6f5ecda2247a2b363d25f493c7b05a 1 parent 9ecd978
Julien Phalip authored July 21, 2012
8  django/core/urlresolvers.py
@@ -160,10 +160,16 @@ def regex(self):
160 160
         language_code = get_language()
161 161
         if language_code not in self._regex_dict:
162 162
             if isinstance(self._regex, basestring):
163  
-                compiled_regex = re.compile(self._regex, re.UNICODE)
  163
+                regex = self._regex
164 164
             else:
165 165
                 regex = force_unicode(self._regex)
  166
+            try:
166 167
                 compiled_regex = re.compile(regex, re.UNICODE)
  168
+            except re.error, e:
  169
+                raise ImproperlyConfigured(
  170
+                    u'"%s" is not a valid regular expression: %s' %
  171
+                    (regex, unicode(e)))
  172
+
167 173
             self._regex_dict[language_code] = compiled_regex
168 174
         return self._regex_dict[language_code]
169 175
 
2  tests/regressiontests/urlpatterns_reverse/erroneous_urls.py
@@ -11,4 +11,6 @@
11 11
     url(r'uncallable/$', 'regressiontests.urlpatterns_reverse.views.uncallable'),
12 12
     # Module does not exist
13 13
     url(r'missing_outer/$', 'regressiontests.urlpatterns_reverse.missing_module.missing_view'),
  14
+    # Regex contains an error (refs #6170)
  15
+    url(r'(regex_error/$', 'regressiontestes.urlpatterns_reverse.views.empty_view'),
14 16
 )
8  tests/regressiontests/urlpatterns_reverse/tests.py
@@ -511,3 +511,11 @@ def test_erroneous_resolve(self):
511 511
         self.assertRaises(ViewDoesNotExist, self.client.get, '/missing_outer/')
512 512
         self.assertRaises(ViewDoesNotExist, self.client.get, '/uncallable/')
513 513
 
  514
+    def test_erroneous_reverse(self):
  515
+        """
  516
+        Ensure that a useful exception is raised when a regex is invalid in the
  517
+        URLConf.
  518
+        Refs #6170.
  519
+        """
  520
+        # The regex error will be hit before NoReverseMatch can be raised
  521
+        self.assertRaises(ImproperlyConfigured, reverse, 'whatever blah blah')

0 notes on commit 1af0271

Simon Charette

Maybe we should use the except re.error as e syntax here?

Please sign in to comment.
Something went wrong with that request. Please try again.