From 18185724e614fb3449612ce3df811af32fbc0582 Mon Sep 17 00:00:00 2001 From: Bouke Haarsma Date: Wed, 20 Nov 2013 18:50:36 +0100 Subject: [PATCH] Fixed #21443 -- Cannot show debug info on PY3's importlib Thanks productions@zaziork.co.uk for the review. --- django/views/debug.py | 5 ++++- tests/view_tests/tests/test_debug.py | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/django/views/debug.py b/django/views/debug.py index 2706da6268..e3f399ef9d 100644 --- a/django/views/debug.py +++ b/django/views/debug.py @@ -388,7 +388,10 @@ class ExceptionReporter(object): """ source = None if loader is not None and hasattr(loader, "get_source"): - source = loader.get_source(module_name) + try: + source = loader.get_source(module_name) + except ImportError: + pass if source is not None: source = source.splitlines() if source is None: diff --git a/tests/view_tests/tests/test_debug.py b/tests/view_tests/tests/test_debug.py index fbbcdc9a40..3805ce50e0 100644 --- a/tests/view_tests/tests/test_debug.py +++ b/tests/view_tests/tests/test_debug.py @@ -19,6 +19,7 @@ from django.test import TestCase, RequestFactory from django.test.utils import (override_settings, setup_test_template_loader, restore_template_loaders) from django.utils.encoding import force_text, force_bytes +from django.utils import importlib, six from django.views.debug import ExceptionReporter from .. import BrokenException, except_args @@ -239,6 +240,21 @@ class ExceptionReporterTests(TestCase): self.assertIn('

Request information

', html) self.assertIn('

Request data not supplied

', html) + @skipIf(six.PY2, 'Bug manifests on PY3 only') + def test_unfrozen_importlib(self): + """ + importlib is not a frozen app, but its loader thinks it's frozen which + results in an ImportError on Python 3. Refs #21443. + """ + try: + request = self.rf.get('/test_view/') + importlib.import_module('abc.def.invalid.name') + except Exception: + exc_type, exc_value, tb = sys.exc_info() + reporter = ExceptionReporter(request, exc_type, exc_value, tb) + html = reporter.get_traceback_html() + self.assertIn('

ImportError at /test_view/

', html) + class PlainTextReportTests(TestCase): rf = RequestFactory()