From da36e3222415fd04332a529623f88f908be8c2a6 Mon Sep 17 00:00:00 2001 From: Karen Tracey Date: Thu, 15 Apr 2010 19:57:09 +0000 Subject: [PATCH] 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 --- django/template/__init__.py | 20 ++++---- .../templates/eggs/tagsegg.egg | Bin 0 -> 2581 bytes tests/regressiontests/templates/tests.py | 43 ++++++++++++++++++ 3 files changed, 53 insertions(+), 10 deletions(-) create mode 100755 tests/regressiontests/templates/eggs/tagsegg.egg diff --git a/django/template/__init__.py b/django/template/__init__.py index 00949d6e59..c3167861fd 100644 --- a/django/template/__init__.py +++ b/django/template/__init__.py @@ -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: diff --git a/tests/regressiontests/templates/eggs/tagsegg.egg b/tests/regressiontests/templates/eggs/tagsegg.egg new file mode 100755 index 0000000000000000000000000000000000000000..3941914b81bc70455e492330286c775cb23d483c GIT binary patch literal 2581 zcmWIWW@Zs#U|`^2XxrImGnwtW5i^j-2*k2L?CS2W>*?p_ub+}ykeZj0nwMM|pOcxF zU94A9QSz9HApk|Umux&+DNxs5AQnW{9pDWy=CiM^r;n$O=M`RWU9EFx&TkGfxMKX^ z=_l_qzFUJdJbm>2eEpw4b(+efclNBW&Z;dz9A7_w`^^9S@zYNy&ih`~_3-g`6)M)z zJ$XX^kiZ4sv%Y7}I0}6`>Em}++wI!RNl|lRrbGrXZ4=a!nlL$H(zN&~-6||-Ze+UV z_qqn?Hjo!V{)YQ7*grJL*))zc8EU`i9YI_v{vHtrQ-dEw)!&qJbq`t)I0aXn|SC7DIMzNq#|mPHI_d4#>UQ(2zrq59HWHiI1wx0^Q=ov{aNB+_|etQ3# z8RbUIA_hzwnFYi*GjxiuwI%RQ=$Oyu!Pl#CjYY_F=}b#aLr=|_M^&qy&a6D?Cgy7H>=pitzWRIg?Ys-HFNu=ChAxDQql}h@F2P8;@UnNJu8-Z@<4Zh zlCKhydrDGs3vv=mQbF$1Pb$jKPR)x4dJX6!Z|zg(Lp@G&e&USo34N`=4h_$< zx+j1s^(lIoD*Zd#IR$9!aUfPE%6PEvUx)uQe4oOmk-(P3uAnEzD5U9dffxTlGjpcQSTg0zBcZ*Y&e$X`o^j>Nml>0ed`U9kHRV6j@2Pd_yx-Yw_O92WA=3&N zOm9DVwtUL7CAS)70(gvskiskq80`;y-kmWAdIl8#I6YOKUzDAhmySEo;9>Si%ggt? zj-ID((1up-S3gtKL{-H?voeYb8IkM-6*pauCLvpZcD@5*6(EMC9hA62wHF+k*}gB4 zLNkm3Jv2|9m~isQg);{Pj-HtS3E8=@kmVDY2oG1LM;jY^!12z{BpH8*Po>6aa+grI z*1=QVL6?oYy@R?<95!(*x*_DDGBxelk|Qch8AM-9O@AgSeRRpP<;SO}EE8|eT;Zxv z=Tli1Se6!7WfS1Eo*`+IZ(f~Orr#>r6RAgi9Gn_Bk$kO-mdO-w`5I4-W9?QI$N=i= z1!6@a^n-&;d(9U-+0Tp_Pxec>pAAzEMT=NPlPl`7@_=I{U9{uh4nOkhS&XS%1y5 zo?DqCfr&~0=uJi@5pW@Zy*2^56%4jDf+%>&i>+=!HwwLKff&TVu&vP!*(g{=gRULD z)JJH4gsdH@4nQ{ny(C7MFacPT!2N%gzy);Fbrvz+5VSq4gDG{at9mmQB P(!vXbn}H$Z1Lgq$#NZ>C literal 0 HcmV?d00001 diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py index 372d2ab1c2..33d0650c7c 100644 --- a/tests/regressiontests/templates/tests.py +++ b/tests/regressiontests/templates/tests.py @@ -1287,5 +1287,48 @@ class Templates(unittest.TestCase): 'autoescape-filtertag01': ("{{ first }}{% filter safe %}{{ first }} x"}, 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()