Ticket #8809: django8809-urlerror.2.diff
File django8809-urlerror.2.diff, 8.2 KB (added by , 14 years ago) |
---|
-
django/core/urlresolvers.py
162 162 return self._callback 163 163 try: 164 164 self._callback = get_callable(self._callback_str) 165 except ImportError, e: 166 mod_name, _ = get_mod_func(self._callback_str) 167 raise ViewDoesNotExist("Could not import %s. Error was: %s" % (mod_name, str(e))) 168 except AttributeError, e: 165 except (ImportError, AttributeError), e: 169 166 mod_name, func_name = get_mod_func(self._callback_str) 170 raise ViewDoesNotExist("Tried %s in module %s. Error was: %s" % (func_name, mod_name, str(e))) 167 debug_info = '' 168 if settings.DEBUG: 169 if hasattr(self, '_origin_line'): # extra debug info available 170 debug_info = ' from line "%s" at %s:%s' % self._origin_line 171 elif hasattr(self, '_origin_module'): 172 debug_info = ' from pattern "%s" in %s' % ((self.regex.pattern,) + self._origin_module) 173 if isinstance(e, ImportError): 174 raise ViewDoesNotExist("Could not import %s%s. Error was: %s" % (mod_name, debug_info, str(e))) 175 elif isinstance(e, AttributeError): 176 raise ViewDoesNotExist("Tried %s in module %s%s. Error was: %s" % (func_name, mod_name, debug_info, str(e))) 171 177 return self._callback 172 178 callback = property(_get_callback) 173 179 -
django/conf/urls/defaults.py
1 from django.conf import settings 1 2 from django.core.urlresolvers import RegexURLPattern, RegexURLResolver 2 3 from django.core.exceptions import ImproperlyConfigured 3 4 … … 17 18 urlconf_module = arg 18 19 return (urlconf_module, app_name, namespace) 19 20 21 def _get_pattern_origin(urlobj): 22 """ Finds out which module called this pattern/url for debug purposes. """ 23 import inspect 24 import os.path 25 current_frame = inspect.currentframe() 26 if current_frame: # currentframe not always supported 27 calling_frames = inspect.getouterframes(current_frame) 28 index = 0 29 while calling_frames[index][3] != '<module>': # find the module 30 index = index + 1 31 calling_frame = calling_frames[index] 32 if index == 2: # direct call from url(), get line details 33 urlobj._origin_line = (calling_frame[4], os.path.normpath(calling_frame[1]), calling_frame[2], ) 34 else: # url() created by patterns(), only show module 35 urlobj._origin_module = (os.path.normpath(calling_frame[1]), ) 36 20 37 def patterns(prefix, *args): 21 38 pattern_list = [] 22 39 for t in args: … … 31 48 if isinstance(view, (list,tuple)): 32 49 # For include(...) processing. 33 50 urlconf_module, app_name, namespace = view 34 returnRegexURLResolver(regex, urlconf_module, kwargs, app_name=app_name, namespace=namespace)51 urlobj = RegexURLResolver(regex, urlconf_module, kwargs, app_name=app_name, namespace=namespace) 35 52 else: 36 53 if isinstance(view, basestring): 37 54 if not view: 38 55 raise ImproperlyConfigured('Empty URL pattern view name not permitted (for pattern %r)' % regex) 39 56 if prefix: 40 57 view = prefix + '.' + view 41 returnRegexURLPattern(regex, view, kwargs, name)58 urlobj = RegexURLPattern(regex, view, kwargs, name) 42 59 60 if settings.DEBUG: # attach debug information to object 61 _get_pattern_origin(urlobj) 62 return urlobj 63 -
tests/regressiontests/urlpatterns_errors/tests.py
1 from django.conf import settings 2 from django.core.urlresolvers import RegexURLResolver, ViewDoesNotExist 3 from django.test import TestCase 4 5 class URLPatternErrorsTestsNonSpecific(TestCase): 6 urls = 'regressiontests.urlpatterns_errors.urls' 7 8 def assertRaisesErrorWithStart(self, error, message, callable, 9 *args, **kwargs): 10 self.assertRaises(error, callable, *args, **kwargs) 11 try: 12 callable(*args, **kwargs) 13 except error, e: # check start of message only 14 self.assertEqual(message, str(e)[:len(message)]) 15 16 def test_resolve_errors_nonspecific(self): 17 """ 18 Resolver should raise exceptions pointing only divulging 19 basic information 20 """ 21 22 olddebug = settings.DEBUG 23 settings.DEBUG = False 24 25 resolver = RegexURLResolver(r'^', self.urls) 26 27 # Import errors 28 self.assertRaisesErrorWithStart(ViewDoesNotExist, 29 "Could not import nonexistent. Error was:", resolver.resolve, 30 'first-urled/') 31 self.assertRaisesErrorWithStart(ViewDoesNotExist, 32 "Could not import nonexistent. Error was:", resolver.resolve, 33 'second-nonurled/') 34 35 # Attribute errors 36 self.assertRaisesErrorWithStart(ViewDoesNotExist, 37 "Tried nonexistent in module regressiontests.urlpatterns_errors.views.", 38 resolver.resolve, 'third-urled/') 39 self.assertRaisesErrorWithStart(ViewDoesNotExist, 40 "Tried nonexistent in module regressiontests.urlpatterns_errors.views.", 41 resolver.resolve, 'fourth-nonurled/') 42 43 settings.DEBUG = olddebug 44 45 class URLPatternErrorsSpecificTests(TestCase): 46 urls = 'regressiontests.urlpatterns_errors.urls' 47 48 def assertRaisesErrorWithStart(self, error, message, callable, 49 *args, **kwargs): 50 self.assertRaises(error, callable, *args, **kwargs) 51 try: 52 callable(*args, **kwargs) 53 except error, e: # check start of message only 54 self.assertEqual(message, str(e)[:len(message)]) 55 56 def test_resolve_errors_specific(self): 57 """ 58 Resolver should raise exceptions pointing to the urls module 59 if that information is available 60 """ 61 62 olddebug = settings.DEBUG 63 settings.DEBUG = True 64 65 resolver = RegexURLResolver(r'^', self.urls) 66 67 # Import errors 68 self.assertRaisesErrorWithStart(ViewDoesNotExist, 69 "Could not import nonexistent from line", resolver.resolve, 70 'first-urled/') 71 self.assertRaisesErrorWithStart(ViewDoesNotExist, 72 "Could not import nonexistent from pattern", resolver.resolve, 73 'second-nonurled/') 74 75 # Attribute errors 76 self.assertRaisesErrorWithStart(ViewDoesNotExist, 77 "Tried nonexistent in module regressiontests.urlpatterns_errors.views from line", 78 resolver.resolve, 'third-urled/') 79 self.assertRaisesErrorWithStart(ViewDoesNotExist, 80 "Tried nonexistent in module regressiontests.urlpatterns_errors.views from pattern", 81 resolver.resolve, 'fourth-nonurled/') 82 83 settings.DEBUG = olddebug -
tests/regressiontests/urlpatterns_errors/urls.py
1 from django.conf.urls.defaults import url, patterns 2 3 urlpatterns = patterns('', 4 url(r'^first-urled/$', 'nonexistent.view'), 5 (r'^second-nonurled/$', 'nonexistent.view'), 6 url(r'^third-urled/$', 'regressiontests.urlpatterns_errors.views.nonexistent'), 7 (r'^fourth-nonurled/$', 'regressiontests.urlpatterns_errors.views.nonexistent'), 8 )