summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Friedl <joe@joefriedl.net>2013-03-30 21:26:29 -0400
committerSimon Charette <charette.s@gmail.com>2013-03-31 23:00:06 -0400
commit2d0db67813d457dc890a2287d9e98593565d650f (patch)
tree989a45f429b51ade52aec89f45493765bd74b386
parent3ff3212713403c5bb8c5b067d2f00d62cf111036 (diff)
Fixed #20167 -- Preserve the traceback of `ImportError`s in `import_by_path`.
Thanks @carljm for the review.
-rw-r--r--django/utils/module_loading.py7
-rw-r--r--tests/utils_tests/module_loading.py10
2 files changed, 15 insertions, 2 deletions
diff --git a/django/utils/module_loading.py b/django/utils/module_loading.py
index 01996743cd..ede585e562 100644
--- a/django/utils/module_loading.py
+++ b/django/utils/module_loading.py
@@ -3,6 +3,7 @@ import os
import sys
from django.core.exceptions import ImproperlyConfigured
+from django.utils import six
from django.utils.importlib import import_module
@@ -19,8 +20,10 @@ def import_by_path(dotted_path, error_prefix=''):
try:
module = import_module(module_path)
except ImportError as e:
- raise ImproperlyConfigured('%sError importing module %s: "%s"' % (
- error_prefix, module_path, e))
+ msg = '%sError importing module %s: "%s"' % (
+ error_prefix, module_path, e)
+ six.reraise(ImproperlyConfigured, ImproperlyConfigured(msg),
+ sys.exc_info()[2])
try:
attr = getattr(module, class_name)
except AttributeError:
diff --git a/tests/utils_tests/module_loading.py b/tests/utils_tests/module_loading.py
index cd97e1436d..0a905ef2f1 100644
--- a/tests/utils_tests/module_loading.py
+++ b/tests/utils_tests/module_loading.py
@@ -120,6 +120,16 @@ class ModuleImportTestCase(unittest.TestCase):
import_by_path('unexistent.module.path', error_prefix="Foo")
self.assertTrue(str(cm.exception).startswith('Foo'))
+ def test_import_error_traceback(self):
+ """Test preserving the original traceback on an ImportError."""
+ try:
+ import_by_path('test_module.bad_module.content')
+ except ImproperlyConfigured:
+ traceback = sys.exc_info()[2]
+
+ self.assertIsNotNone(traceback.tb_next.tb_next,
+ 'Should have more than the calling frame in the traceback.')
+
class ProxyFinder(object):
def __init__(self):