From de62b8ef4587e8f01462629e2812f06f9ddf71e8 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Thu, 5 Feb 2015 12:14:36 +0100 Subject: [PATCH] Simplified handling of RegexURLResolver.urlconf_module. Also made RegexURLResolver.url_patterns a cached property for consistency and efficiency. --- django/core/urlresolvers.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/django/core/urlresolvers.py b/django/core/urlresolvers.py index d7383a04227..702290b61d5 100644 --- a/django/core/urlresolvers.py +++ b/django/core/urlresolvers.py @@ -19,7 +19,7 @@ from django.core.exceptions import ImproperlyConfigured, ViewDoesNotExist from django.utils.datastructures import MultiValueDict from django.utils.deprecation import RemovedInDjango20Warning from django.utils.encoding import force_str, force_text, iri_to_uri -from django.utils.functional import lazy +from django.utils.functional import cached_property, lazy from django.utils.http import RFC3986_SUBDELIMS, urlquote from django.utils.module_loading import module_has_submodule from django.utils.regex_helper import normalize @@ -252,10 +252,10 @@ class RegexURLPattern(LocaleRegexProvider): class RegexURLResolver(LocaleRegexProvider): def __init__(self, regex, urlconf_name, default_kwargs=None, app_name=None, namespace=None): LocaleRegexProvider.__init__(self, regex) - # urlconf_name is a string representing the module containing URLconfs. + # urlconf_name is the dotted Python path to the module defining + # urlpatterns. It may also be an object with an urlpatterns attribute + # or urlpatterns itself. self.urlconf_name = urlconf_name - if not isinstance(urlconf_name, six.string_types): - self._urlconf_module = self.urlconf_name self.callback = None self.default_kwargs = default_kwargs or {} self.namespace = namespace @@ -389,15 +389,14 @@ class RegexURLResolver(LocaleRegexProvider): raise Resolver404({'tried': tried, 'path': new_path}) raise Resolver404({'path': path}) - @property + @cached_property def urlconf_module(self): - try: - return self._urlconf_module - except AttributeError: - self._urlconf_module = import_module(self.urlconf_name) - return self._urlconf_module + if isinstance(self.urlconf_name, six.string_types): + return import_module(self.urlconf_name) + else: + return self.urlconf_name - @property + @cached_property def url_patterns(self): # urlconf_module might be a valid set of patterns, so we default to it patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)