From 89fc144dedc737a79929231438f035b1d4a993c9 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Sun, 27 Dec 2020 10:23:16 -0500 Subject: [PATCH] Fixed #27827 -- Used "raise from" when raising InvalidTemplateLibrary exceptions in get_package_libraries(). This change sets the __cause__ attribute to raised exceptions and makes small cleanups in error messages. --- django/template/backends/django.py | 2 +- tests/template_backends/test_django.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/django/template/backends/django.py b/django/template/backends/django.py index 80d11d3cdd..d99631cc19 100644 --- a/django/template/backends/django.py +++ b/django/template/backends/django.py @@ -123,7 +123,7 @@ def get_package_libraries(pkg): raise InvalidTemplateLibrary( "Invalid template library specified. ImportError raised when " "trying to load '%s': %s" % (entry[1], e) - ) + ) from e if hasattr(module, 'register'): yield entry[1] diff --git a/tests/template_backends/test_django.py b/tests/template_backends/test_django.py index 6f5035c741..047af67df5 100644 --- a/tests/template_backends/test_django.py +++ b/tests/template_backends/test_django.py @@ -104,13 +104,14 @@ class DjangoTemplatesTests(TemplateStringsTests): InvalidTemplateLibrary, "ImportError raised when trying to load " "'template_backends.apps.importerror.templatetags.broken_tags'" - ): + ) as cm: DjangoTemplates({ 'DIRS': [], 'APP_DIRS': False, 'NAME': 'django', 'OPTIONS': {}, }) + self.assertIsInstance(cm.exception.__cause__, ImportError) def test_builtins_discovery(self): engine = DjangoTemplates({