From fd7c0a1a7cce9796901f32bed5a840918ae6edfe Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Thu, 2 Feb 2006 05:11:21 +0000 Subject: [PATCH] magic-removal: Merged to [2226] git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2227 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/template/loader.py | 40 ++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/django/template/loader.py b/django/template/loader.py index 631b3f255c..6af9288c55 100644 --- a/django/template/loader.py +++ b/django/template/loader.py @@ -25,23 +25,7 @@ from django.template import Origin, StringOrigin, Template, Context, TemplateDoe from django.conf import settings from django.conf import settings -template_source_loaders = [] -for path in settings.TEMPLATE_LOADERS: - i = path.rfind('.') - module, attr = path[:i], path[i+1:] - try: - mod = __import__(module, globals(), locals(), [attr]) - except ImportError, e: - raise ImproperlyConfigured, 'Error importing template source loader %s: "%s"' % (module, e) - try: - func = getattr(mod, attr) - except AttributeError: - raise ImproperlyConfigured, 'Module "%s" does not define a "%s" callable template source loader' % (module, attr) - if not func.is_usable: - import warnings - warnings.warn("Your TEMPLATE_LOADERS setting includes %r, but your Python installation doesn't support that type of template loading. Consider removing that line from TEMPLATE_LOADERS." % path) - else: - template_source_loaders.append(func) +template_source_loaders = None class LoaderOrigin(Origin): def __init__(self, display_name, loader, name, dirs): @@ -58,6 +42,28 @@ def make_origin(display_name, loader, name, dirs): return None def find_template_source(name, dirs=None): + # Calculate template_source_loaders the first time the function is executed + # because putting this logic in the module-level namespace may cause + # circular import errors. See Django ticket #1292. + global template_source_loaders + if template_source_loaders is None: + template_source_loaders = [] + for path in settings.TEMPLATE_LOADERS: + i = path.rfind('.') + module, attr = path[:i], path[i+1:] + try: + mod = __import__(module, globals(), locals(), [attr]) + except ImportError, e: + raise ImproperlyConfigured, 'Error importing template source loader %s: "%s"' % (module, e) + try: + func = getattr(mod, attr) + except AttributeError: + raise ImproperlyConfigured, 'Module "%s" does not define a "%s" callable template source loader' % (module, attr) + if not func.is_usable: + import warnings + warnings.warn("Your TEMPLATE_LOADERS setting includes %r, but your Python installation doesn't support that type of template loading. Consider removing that line from TEMPLATE_LOADERS." % path) + else: + template_source_loaders.append(func) for loader in template_source_loaders: try: source, display_name = loader(name, dirs)