Issue #1559549: Add 'name' and 'path' attributes to ImportError.
Currently import does not use these attributes as they are planned
for use by importlib (which will be another commit).
Thanks to Filip GruszczyĆski for the initial patch and Brian Curtin
for refining it.
diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
index 42536d3..39ff85f 100644
--- a/Lib/test/test_exceptions.py
+++ b/Lib/test/test_exceptions.py
@@ -902,8 +902,30 @@
self.assertEqual(cm.exception.errno, errno.ENOTDIR, cm.exception)
+class ImportErrorTests(unittest.TestCase):
+
+ def test_attributes(self):
+ # Setting 'name' and 'path' should not be a problem.
+ exc = ImportError('test')
+ self.assertIsNone(exc.name)
+ self.assertIsNone(exc.path)
+
+ exc = ImportError('test', name='somemodule')
+ self.assertEqual(exc.name, 'somemodule')
+ self.assertIsNone(exc.path)
+
+ exc = ImportError('test', path='somepath')
+ self.assertEqual(exc.path, 'somepath')
+ self.assertIsNone(exc.name)
+
+ exc = ImportError('test', path='somepath', name='somename')
+ self.assertEqual(exc.name, 'somename')
+ self.assertEqual(exc.path, 'somepath')
+
+
+
def test_main():
- run_unittest(ExceptionTests)
+ run_unittest(ExceptionTests, ImportErrorTests)
if __name__ == '__main__':
unittest.main()