Ticket #10802: django-10802.2.diff

File django-10802.2.diff, 5.1 KB (added by bpeschier, 4 years ago)
  • django/core/urlresolvers.py

    diff -r f7582637764c django/core/urlresolvers.py
    a b  
    1717from django.utils.encoding import iri_to_uri, force_unicode, smart_str
    1818from django.utils.functional import memoize, lazy
    1919from django.utils.importlib import import_module
     20from django.utils.module_loading import module_has_submodule
    2021from django.utils.regex_helper import normalize
    2122
    2223_resolver_cache = {} # Maps URLconf modules to RegexURLResolver instances.
     
    8384    during the import fail and the string is returned.
    8485    """
    8586    if not callable(lookup_view):
     87        mod_name, func_name = get_mod_func(lookup_view)
    8688        try:
    87             # Bail early for non-ASCII strings (they can't be functions).
    88             lookup_view = lookup_view.encode('ascii')
    89             mod_name, func_name = get_mod_func(lookup_view)
    9089            if func_name != '':
    9190                lookup_view = getattr(import_module(mod_name), func_name)
    9291                if not callable(lookup_view):
    93                     raise AttributeError("'%s.%s' is not a callable." % (mod_name, func_name))
    94         except (ImportError, AttributeError):
     92                    raise ViewDoesNotExist("Could not import %s.%s. View is not callable." % (mod_name, func_name))
     93        except AttributeError:
     94            if not can_fail:
     95                raise ViewDoesNotExist("Could not import %s. View does not exist in module %s." % (lookup_view, mod_name))
     96        except ImportError:
     97            ownermod, submod = get_mod_func(mod_name)
     98            if not can_fail and submod != '' and not module_has_submodule(import_module(ownermod), submod):
     99                raise ViewDoesNotExist("Could not import %s. Owning module %s does not exist." % (lookup_view, mod_name))
    95100            if not can_fail:
    96101                raise
    97         except UnicodeEncodeError:
    98             pass
    99102    return lookup_view
    100103get_callable = memoize(get_callable, _callable_cache, 1)
    101104
     
    160163    def _get_callback(self):
    161164        if self._callback is not None:
    162165            return self._callback
    163         try:
    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:
    169             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)))
     166
     167        self._callback = get_callable(self._callback_str)
    171168        return self._callback
    172169    callback = property(_get_callback)
    173170
     
    291288            # Lazy import, since urls.defaults imports this file
    292289            from django.conf.urls import defaults
    293290            callback = getattr(defaults, 'handler%s' % view_type)
    294         try:
    295             return get_callable(callback), {}
    296         except (ImportError, AttributeError), e:
    297             raise ViewDoesNotExist("Tried %s. Error was: %s" % (callback, str(e)))
     291        return get_callable(callback), {}
    298292
    299293    def resolve404(self):
    300294        return self._resolve_special('404')
  • tests/regressiontests/urlpatterns_reverse/tests.py

    diff -r f7582637764c tests/regressiontests/urlpatterns_reverse/tests.py
    a b  
    22Unit tests for reverse URL lookups.
    33"""
    44from django.conf import settings
    5 from django.core.exceptions import ImproperlyConfigured
     5from django.core.exceptions import ImproperlyConfigured, ViewDoesNotExist
    66from django.core.urlresolvers import reverse, resolve, NoReverseMatch,\
    77                                     Resolver404, ResolverMatch,\
    88                                     RegexURLResolver, RegexURLPattern
     
    470470            self.assertEqual(match[0], func)
    471471            self.assertEqual(match[1], args)
    472472            self.assertEqual(match[2], kwargs)
     473
     474class ErroneousViewTests(TestCase):
     475    urls = 'regressiontests.urlpatterns_reverse.erroneous_urls'
     476
     477    def test_erroneous_resolve(self):
     478        self.assertRaises(ImportError, self.client.get, '/erroneous_inner/')
     479        self.assertRaises(ImportError, self.client.get, '/erroneous_outer/')
     480        self.assertRaises(ViewDoesNotExist, self.client.get, '/missing_inner/')
     481        self.assertRaises(ViewDoesNotExist, self.client.get, '/missing_outer/')
     482        self.assertRaises(ViewDoesNotExist, self.client.get, '/uncallable/')
     483
  • tests/regressiontests/urlpatterns_reverse/views.py

    diff -r f7582637764c tests/regressiontests/urlpatterns_reverse/views.py
    a b  
    1616def defaults_view(request, arg1, arg2):
    1717    pass
    1818
     19def erroneous_view(request):
     20    import non_existent
     21
     22uncallable = "Can I be a view? Pleeeease?"
     23
    1924class ViewClass(object):
    2025    def __call__(self, request, *args, **kwargs):
    2126        return HttpResponse('')
Back to Top