recursiveImport now has much more helpful error messages
authorAndy Robinson <andy@reportlab.com>
Wed, 27 Mar 2013 13:19:33 +0000
changeset 3668 d8a42e00c079
parent 3667 ab79820991f9
child 3669 40848d552920
recursiveImport now has much more helpful error messages
src/reportlab/lib/utils.py
tests/test_lib_utils.py
tests/unimportable.py
--- a/src/reportlab/lib/utils.py	Sun Mar 24 07:42:19 2013 +0000
+++ b/src/reportlab/lib/utils.py	Wed Mar 27 13:19:33 2013 +0000
@@ -291,11 +291,16 @@
         return m
     except ImportError:
         sys.path = opath
-        msg = "recursiveimport(%s,baseDir=%s) failed" % (modulename,baseDir)
+        msg = "Could not import '%s'" % modulename
         if baseDir:
-            msg = msg + " under paths '%s'" % repr(path)
+            msg = msg + " under %s" % baseDir
         raise ImportError, msg
 
+    except Exception, e:
+        msg = "Exception raised while importing '%s': %s" % (modulename, e.message)
+        raise ImportError, msg
+        
+
 def recursiveGetAttr(obj, name):
     "Can call down into e.g. object1.object2[4].attr"
     return eval(name, obj.__dict__)
--- a/tests/test_lib_utils.py	Sun Mar 24 07:42:19 2013 +0000
+++ b/tests/test_lib_utils.py	Wed Mar 27 13:19:33 2013 +0000
@@ -119,6 +119,32 @@
         b = getStringIO(_rel_open_and_read('../docs/images/Edit_Prefs.gif'))
         b = open_and_read(b)
 
+
+    def testRecursiveImportErrors(self):
+        "check we get useful error messages"
+        try:
+            m1 = recursiveImport('reportlab.pdfgen.brush')
+            self.fail("Imported a nonexistent module")
+        except ImportError, e:
+            self.assertEquals(e.message, "Could not import 'reportlab.pdfgen.brush'")
+            
+        try:
+            m1 = recursiveImport('totally.non.existent')
+            self.fail("Imported a nonexistent module")
+        except ImportError, e:
+            self.assertEquals(e.message, "Could not import 'totally.non.existent'")
+
+        try:
+            #import a module in the 'tests' directory with a bug
+            m1 = recursiveImport('unimportable')
+            self.fail("Imported a buggy module")
+        except ImportError, e:
+            self.assert_('integer division or modulo by zero' in e.message)
+
+
+
+
+
 def makeSuite():
     return makeSuiteForClasses(ImporterTestCase)
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/unimportable.py	Wed Mar 27 13:19:33 2013 +0000
@@ -0,0 +1,10 @@
+#module you cannot import
+"""
+This is a fixture used for testing a dynamic import function.
+
+It is supposed to produce an error when imported.
+"""
+
+value = 100 / 0
+
+