summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAymeric Augustin <aymeric.augustin@m4x.org>2014-11-18 21:50:52 +0100
committerAymeric Augustin <aymeric.augustin@m4x.org>2014-11-19 21:35:39 +0100
commita97e72aaabd1d065d49f909be20f0f3367a346d4 (patch)
tree7fbf83ca74b2a45816503c49e0c13131c7fd1c98
parente23240474b4c7d630a4224a558f2e47a4b957cd1 (diff)
Simplified caching of templatetags modules.
-rw-r--r--django/template/base.py32
-rw-r--r--django/test/signals.py4
-rw-r--r--tests/template_tests/tests.py5
3 files changed, 16 insertions, 25 deletions
diff --git a/django/template/base.py b/django/template/base.py
index ee33dacda2..7c69e26088 100644
--- a/django/template/base.py
+++ b/django/template/base.py
@@ -10,6 +10,7 @@ from django.apps import apps
from django.conf import settings
from django.template.context import (BaseContext, Context, RequestContext, # NOQA: imported for backwards compatibility
ContextPopException)
+from django.utils import lru_cache
from django.utils.deprecation import RemovedInDjango20Warning
from django.utils.itercompat import is_iterable
from django.utils.text import (smart_split, unescape_string_literal,
@@ -1296,32 +1297,27 @@ def import_library(taglib_module):
"a variable named 'register'" %
taglib_module)
-templatetags_modules = []
-
+@lru_cache.lru_cache()
def get_templatetags_modules():
"""
Return the list of all available template tag modules.
Caches the result for faster access.
"""
- global templatetags_modules
- if not templatetags_modules:
- _templatetags_modules = []
- # Populate list once per process. Mutate the local list first, and
- # then assign it to the global name to ensure there are no cases where
- # two threads try to populate it simultaneously.
+ templatetags_modules_candidates = ['django.templatetags']
+ templatetags_modules_candidates += [
+ '%s.templatetags' % app_config.name
+ for app_config in apps.get_app_configs()]
- templatetags_modules_candidates = ['django.templatetags']
- templatetags_modules_candidates += ['%s.templatetags' % app_config.name
- for app_config in apps.get_app_configs()]
- for templatetag_module in templatetags_modules_candidates:
- try:
- import_module(templatetag_module)
- _templatetags_modules.append(templatetag_module)
- except ImportError:
- continue
- templatetags_modules = _templatetags_modules
+ templatetags_modules = []
+ for templatetag_module in templatetags_modules_candidates:
+ try:
+ import_module(templatetag_module)
+ except ImportError:
+ continue
+ else:
+ templatetags_modules.append(templatetag_module)
return templatetags_modules
diff --git a/django/test/signals.py b/django/test/signals.py
index c38dd80688..d8c4aa93ae 100644
--- a/django/test/signals.py
+++ b/django/test/signals.py
@@ -37,8 +37,8 @@ def update_installed_apps(**kwargs):
from django.core.management import get_commands
get_commands.cache_clear()
# Rebuild templatetags module cache.
- from django.template import base as mod
- mod.templatetags_modules = []
+ from django.template.base import get_templatetags_modules
+ get_templatetags_modules.cache_clear()
# Rebuild get_app_template_dirs cache.
from django.template.utils import get_app_template_dirs
get_app_template_dirs.cache_clear()
diff --git a/tests/template_tests/tests.py b/tests/template_tests/tests.py
index b8097fbde1..80d7b727d6 100644
--- a/tests/template_tests/tests.py
+++ b/tests/template_tests/tests.py
@@ -1855,11 +1855,6 @@ class TemplateTagLoading(TestCase):
def setUp(self):
self.egg_dir = '%s/eggs' % os.path.dirname(upath(__file__))
- self.old_tag_modules = template_base.templatetags_modules
- template_base.templatetags_modules = []
-
- def tearDown(self):
- template_base.templatetags_modules = self.old_tag_modules
def test_load_error(self):
ttext = "{% load broken_tag %}"