Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #13334: Restored ability to load template tags from eggs. Again…

… thanks Ramiro and metzen for pointers on how to find out if a module loaded from an egg has a particular submodule, and Russ for review.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12986 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit da36e3222415fd04332a529623f88f908be8c2a6 1 parent 4604e98
Karen Tracey kmtracey authored
20 django/template/__init__.py
View
@@ -63,6 +63,7 @@
from django.utils.safestring import SafeData, EscapeData, mark_safe, mark_for_escaping
from django.utils.formats import localize
from django.utils.html import escape
+from django.utils.module_loading import module_has_submodule
__all__ = ('Template', 'Context', 'RequestContext', 'compile_string')
@@ -980,19 +981,18 @@ def import_library(taglib_module):
Verifies that the library contains a 'register' attribute, and
returns that attribute as the representation of the library
"""
- # We need to be able to tell the difference between a tag library that
- # doesn't exist, and a tag library with errors in it.
- # find_module() finds, but doesn't actually load the module requested.
- # If it raises ImportError, it means the module doesn't exist.
- # If you then use load_module(), any ImportError is guaranteed to be
- # an actual import problem with the module.
app_path, taglib = taglib_module.rsplit('.',1)
app_module = import_module(app_path)
try:
- imp.find_module(taglib, app_module.__path__)
- except ImportError,e:
- return None
- mod = import_module(taglib_module)
+ mod = import_module(taglib_module)
+ except ImportError, e:
+ # If the ImportError is because the taglib submodule does not exist, that's not
+ # an error that should be raised. If the submodule exists and raised an ImportError
+ # on the attempt to load it, that we want to raise.
+ if not module_has_submodule(app_module, taglib):
+ return None
+ else:
+ raise InvalidTemplateLibrary("ImportError raised loading %s: %s" % (taglib_module, e))
try:
return mod.register
except AttributeError:
BIN  tests/regressiontests/templates/eggs/tagsegg.egg
View
Binary file not shown
43 tests/regressiontests/templates/tests.py
View
@@ -1287,5 +1287,48 @@ def get_template_tests(self):
'autoescape-filtertag01': ("{{ first }}{% filter safe %}{{ first }} x<y{% endfilter %}", {"first": "<a>"}, template.TemplateSyntaxError),
}
+
+class TemplateTagLoading(unittest.TestCase):
+
+ def setUp(self):
+ self.old_path = sys.path
+ self.old_apps = settings.INSTALLED_APPS
+ self.egg_dir = '%s/eggs' % os.path.dirname(__file__)
+ self.old_tag_modules = template.templatetags_modules
+ template.templatetags_modules = []
+
+ def tearDown(self):
+ settings.INSTALLED_APPS = self.old_apps
+ sys.path = self.old_path
+ template.templatetags_modules = self.old_tag_modules
+
+ def test_load_error(self):
+ ttext = "{% load broken_tag %}"
+ self.assertRaises(template.TemplateSyntaxError, template.Template, ttext)
+ try:
+ template.Template(ttext)
+ except template.TemplateSyntaxError, e:
+ self.assertTrue('ImportError' in e.args[0])
+ self.assertTrue('Xtemplate' in e.args[0])
+
+ def test_load_error_egg(self):
+ ttext = "{% load broken_egg %}"
+ egg_name = '%s/tagsegg.egg' % self.egg_dir
+ sys.path.append(egg_name)
+ settings.INSTALLED_APPS = ('tagsegg',)
+ self.assertRaises(template.TemplateSyntaxError, template.Template, ttext)
+ try:
+ template.Template(ttext)
+ except template.TemplateSyntaxError, e:
+ self.assertTrue('ImportError' in e.args[0])
+ self.assertTrue('Xtemplate' in e.args[0])
+
+ def test_load_working_egg(self):
+ ttext = "{% load working_egg %}"
+ egg_name = '%s/tagsegg.egg' % self.egg_dir
+ sys.path.append(egg_name)
+ settings.INSTALLED_APPS = ('tagsegg',)
+ t = template.Template(ttext)
+
if __name__ == "__main__":
unittest.main()
Please sign in to comment.
Something went wrong with that request. Please try again.