Ticket #10802: django-10802.3.diff

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

    diff -r 41ee28cb9212 django/core/urlresolvers.py
    a b  
    1616from django.utils.encoding import iri_to_uri, force_unicode, smart_str
    1717from django.utils.functional import memoize, lazy
    1818from django.utils.importlib import import_module
     19from django.utils.module_loading import module_has_submodule
    1920from django.utils.regex_helper import normalize
    2021from django.utils.translation import get_language
    2122
     
    8485    during the import fail and the string is returned.
    8586    """
    8687    if not callable(lookup_view):
     88        mod_name, func_name = get_mod_func(lookup_view)
    8789        try:
    88             # Bail early for non-ASCII strings (they can't be functions).
    89             lookup_view = lookup_view.encode('ascii')
    90             mod_name, func_name = get_mod_func(lookup_view)
    9190            if func_name != '':
    9291                lookup_view = getattr(import_module(mod_name), func_name)
    9392                if not callable(lookup_view):
    94                     raise AttributeError("'%s.%s' is not a callable." % (mod_name, func_name))
    95         except (ImportError, AttributeError):
     93                    raise ViewDoesNotExist("Could not import %s.%s. View is not callable." % (mod_name, func_name))
     94        except AttributeError:
     95            if not can_fail:
     96                raise ViewDoesNotExist("Could not import %s. View does not exist in module %s." % (lookup_view, mod_name))
     97        except ImportError:
     98            ownermod, submod = get_mod_func(mod_name)
     99            if not can_fail and submod != '' and not module_has_submodule(import_module(ownermod), submod):
     100                raise ViewDoesNotExist("Could not import %s. Owning module %s does not exist." % (lookup_view, mod_name))
    96101            if not can_fail:
    97102                raise
    98         except UnicodeEncodeError:
    99             pass
    100103    return lookup_view
    101104get_callable = memoize(get_callable, _callable_cache, 1)
    102105
     
    192195    def callback(self):
    193196        if self._callback is not None:
    194197            return self._callback
    195         try:
    196             self._callback = get_callable(self._callback_str)
    197         except ImportError, e:
    198             mod_name, _ = get_mod_func(self._callback_str)
    199             raise ViewDoesNotExist("Could not import %s. Error was: %s" % (mod_name, str(e)))
    200         except AttributeError, e:
    201             mod_name, func_name = get_mod_func(self._callback_str)
    202             raise ViewDoesNotExist("Tried %s in module %s. Error was: %s" % (func_name, mod_name, str(e)))
     198
     199        self._callback = get_callable(self._callback_str)
    203200        return self._callback
    204201
    205202class RegexURLResolver(LocaleRegexProvider):
     
    325322            # Lazy import, since urls.defaults imports this file
    326323            from django.conf.urls import defaults
    327324            callback = getattr(defaults, 'handler%s' % view_type)
    328         try:
    329             return get_callable(callback), {}
    330         except (ImportError, AttributeError), e:
    331             raise ViewDoesNotExist("Tried %s. Error was: %s" % (callback, str(e)))
     325        return get_callable(callback), {}
    332326
    333327    def resolve404(self):
    334328        return self._resolve_special('404')
  • new file tests/regressiontests/urlpatterns_reverse/erroneous_urls.py

    diff -r 41ee28cb9212 tests/regressiontests/urlpatterns_reverse/erroneous_urls.py
    - +  
     1
     2from django.conf.urls.defaults import *
     3
     4urlpatterns = patterns('',
     5    # View has erroneous import
     6    url(r'erroneous_inner/$', 'regressiontests.urlpatterns_reverse.views.erroneous_view'),
     7    # Module has erroneous import
     8    url(r'erroneous_outer/$', 'regressiontests.urlpatterns_reverse.erroneous_views_module.erroneous_view'),
     9    # View does not exist
     10    url(r'missing_inner/$', 'regressiontests.urlpatterns_reverse.views.missing_view'),
     11    # View is not callable
     12    url(r'uncallable/$', 'regressiontests.urlpatterns_reverse.views.uncallable'),
     13    # Module does not exist
     14    url(r'missing_outer/$', 'regressiontests.urlpatterns_reverse.missing_module.missing_view'),
     15)
  • new file tests/regressiontests/urlpatterns_reverse/erroneous_views_module.py

    diff -r 41ee28cb9212 tests/regressiontests/urlpatterns_reverse/erroneous_views_module.py
    - +  
     1import non_existent
     2
     3def erroneous_view(request):
     4    pass
  • tests/regressiontests/urlpatterns_reverse/tests.py

    diff -r 41ee28cb9212 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 41ee28cb9212 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