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
This commit is contained in:
Karen Tracey 2010-04-15 19:57:09 +00:00
parent 4604e985ae
commit da36e32224
3 changed files with 53 additions and 10 deletions

View File

@ -63,6 +63,7 @@ from django.utils.translation import ugettext as _
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:

Binary file not shown.

View File

@ -1287,5 +1287,48 @@ class Templates(unittest.TestCase):
'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()