Kaydet (Commit) 1af0271d authored tarafından Julien Phalip's avatar Julien Phalip

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.
üst 9ecd978e
...@@ -160,10 +160,16 @@ class LocaleRegexProvider(object): ...@@ -160,10 +160,16 @@ class LocaleRegexProvider(object):
language_code = get_language() language_code = get_language()
if language_code not in self._regex_dict: if language_code not in self._regex_dict:
if isinstance(self._regex, basestring): if isinstance(self._regex, basestring):
compiled_regex = re.compile(self._regex, re.UNICODE) regex = self._regex
else: else:
regex = force_unicode(self._regex) regex = force_unicode(self._regex)
try:
compiled_regex = re.compile(regex, re.UNICODE) compiled_regex = re.compile(regex, re.UNICODE)
except re.error, e:
raise ImproperlyConfigured(
u'"%s" is not a valid regular expression: %s' %
(regex, unicode(e)))
self._regex_dict[language_code] = compiled_regex self._regex_dict[language_code] = compiled_regex
return self._regex_dict[language_code] return self._regex_dict[language_code]
......
...@@ -11,4 +11,6 @@ urlpatterns = patterns('', ...@@ -11,4 +11,6 @@ urlpatterns = patterns('',
url(r'uncallable/$', 'regressiontests.urlpatterns_reverse.views.uncallable'), url(r'uncallable/$', 'regressiontests.urlpatterns_reverse.views.uncallable'),
# Module does not exist # Module does not exist
url(r'missing_outer/$', 'regressiontests.urlpatterns_reverse.missing_module.missing_view'), url(r'missing_outer/$', 'regressiontests.urlpatterns_reverse.missing_module.missing_view'),
# Regex contains an error (refs #6170)
url(r'(regex_error/$', 'regressiontestes.urlpatterns_reverse.views.empty_view'),
) )
...@@ -511,3 +511,11 @@ class ErroneousViewTests(TestCase): ...@@ -511,3 +511,11 @@ class ErroneousViewTests(TestCase):
self.assertRaises(ViewDoesNotExist, self.client.get, '/missing_outer/') self.assertRaises(ViewDoesNotExist, self.client.get, '/missing_outer/')
self.assertRaises(ViewDoesNotExist, self.client.get, '/uncallable/') self.assertRaises(ViewDoesNotExist, self.client.get, '/uncallable/')
def test_erroneous_reverse(self):
"""
Ensure that a useful exception is raised when a regex is invalid in the
URLConf.
Refs #6170.
"""
# The regex error will be hit before NoReverseMatch can be raised
self.assertRaises(ImproperlyConfigured, reverse, 'whatever blah blah')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment