diff options
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/importlib/_bootstrap.py | 15 | ||||
-rwxr-xr-x | Lib/pydoc.py | 2 | ||||
-rw-r--r-- | Lib/test/exception_hierarchy.txt | 1 | ||||
-rw-r--r-- | Lib/test/test_exceptions.py | 3 | ||||
-rw-r--r-- | Lib/test/test_import.py | 25 | ||||
-rw-r--r-- | Lib/test/test_importlib/import_/test_api.py | 4 | ||||
-rw-r--r-- | Lib/test/test_importlib/import_/test_fromlist.py | 8 | ||||
-rw-r--r-- | Lib/test/test_pydoc.py | 2 | ||||
-rw-r--r-- | Lib/test/test_site.py | 2 |
9 files changed, 35 insertions, 27 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py index 09d8f0e..6607866 100644 --- a/Lib/importlib/_bootstrap.py +++ b/Lib/importlib/_bootstrap.py @@ -1556,7 +1556,11 @@ def _find_and_load_unlocked(name, import_): raise ImportError(msg, name=name) loader = _find_module(name, path) if loader is None: - raise ModuleNotFoundError(_ERR_MSG.format(name), name=name) + exc = ImportError(_ERR_MSG.format(name), name=name) + # TODO(brett): switch to a proper ModuleNotFound exception in Python + # 3.4. + exc._not_found = True + raise exc elif name not in sys.modules: # The parent import may have already imported this module. loader.load_module(name) @@ -1642,12 +1646,15 @@ def _handle_fromlist(module, fromlist, import_): from_name = '{}.{}'.format(module.__name__, x) try: _call_with_frames_removed(import_, from_name) - except ModuleNotFoundError as exc: + except ImportError as exc: # Backwards-compatibility dictates we ignore failed # imports triggered by fromlist for modules that don't # exist. - if exc.name == from_name: - continue + # TODO(brett): In Python 3.4, have import raise + # ModuleNotFound and catch that. + if getattr(exc, '_not_found', False): + if exc.name == from_name: + continue raise return module diff --git a/Lib/pydoc.py b/Lib/pydoc.py index 61d5165..bc64407 100755 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -316,7 +316,7 @@ def safeimport(path, forceload=0, cache={}): elif exc is SyntaxError: # A SyntaxError occurred before we could execute the module. raise ErrorDuringImport(value.filename, info) - elif issubclass(exc, ImportError) and value.name == path: + elif exc is ImportError and value.name == path: # No such module in the path. return None else: diff --git a/Lib/test/exception_hierarchy.txt b/Lib/test/exception_hierarchy.txt index 88e1b81..1c1f69f 100644 --- a/Lib/test/exception_hierarchy.txt +++ b/Lib/test/exception_hierarchy.txt @@ -13,7 +13,6 @@ BaseException +-- BufferError +-- EOFError +-- ImportError - +-- ModuleNotFoundError +-- LookupError | +-- IndexError | +-- KeyError diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py index f0851bd..2f39d4b 100644 --- a/Lib/test/test_exceptions.py +++ b/Lib/test/test_exceptions.py @@ -953,5 +953,8 @@ class ImportErrorTests(unittest.TestCase): self.assertEqual(str(arg), str(exc)) +def test_main(): + run_unittest(ExceptionTests, ImportErrorTests) + if __name__ == '__main__': unittest.main() diff --git a/Lib/test/test_import.py b/Lib/test/test_import.py index 26b5806..bbbac51 100644 --- a/Lib/test/test_import.py +++ b/Lib/test/test_import.py @@ -68,15 +68,7 @@ class ImportTests(unittest.TestCase): def tearDown(self): unload(TESTFN) - def test_import_raises_ModuleNotFoundError(self): - with self.assertRaises(ModuleNotFoundError): - import something_that_should_not_exist_anywhere - - def test_from_import_raises_ModuleNotFoundError(self): - with self.assertRaises(ModuleNotFoundError): - from something_that_should_not_exist_anywhere import blah - with self.assertRaises(ModuleNotFoundError): - from importlib import something_that_should_not_exist_anywhere + setUp = tearDown def test_case_sensitivity(self): # Brief digression to test that import is case-sensitive: if we got @@ -495,7 +487,7 @@ func_filename = func.__code__.co_filename header = f.read(12) code = marshal.load(f) constants = list(code.co_consts) - foreign_code = importlib.import_module.__code__ + foreign_code = test_main.__code__ pos = constants.index(1) constants[pos] = foreign_code code = type(code)(code.co_argcount, code.co_kwonlyargcount, @@ -1021,5 +1013,16 @@ class ImportTracebackTests(unittest.TestCase): importlib.SourceLoader.load_module = old_load_module +def test_main(verbose=None): + run_unittest(ImportTests, PycacheTests, FilePermissionTests, + PycRewritingTests, PathsTests, RelativeImportTests, + OverridingImportBuiltinTests, + ImportlibBootstrapTests, + TestSymbolicallyLinkedPackage, + ImportTracebackTests) + + if __name__ == '__main__': - unittest.main() + # Test needs to be a package, so we can do relative imports. + from test.test_import import test_main + test_main() diff --git a/Lib/test/test_importlib/import_/test_api.py b/Lib/test/test_importlib/import_/test_api.py index 52a3190..bf74937 100644 --- a/Lib/test/test_importlib/import_/test_api.py +++ b/Lib/test/test_importlib/import_/test_api.py @@ -22,10 +22,6 @@ class APITest(unittest.TestCase): """Test API-specific details for __import__ (e.g. raising the right exception when passing in an int for the module name).""" - def test_raises_ModuleNotFoundError(self): - with self.assertRaises(ModuleNotFoundError): - util.import_('some module that does not exist') - def test_name_requires_rparition(self): # Raise TypeError if a non-string is passed in for the module name. with self.assertRaises(TypeError): diff --git a/Lib/test/test_importlib/import_/test_fromlist.py b/Lib/test/test_importlib/import_/test_fromlist.py index a31d216..e440566 100644 --- a/Lib/test/test_importlib/import_/test_fromlist.py +++ b/Lib/test/test_importlib/import_/test_fromlist.py @@ -68,16 +68,16 @@ class HandlingFromlist(unittest.TestCase): self.assertTrue(hasattr(module, 'module')) self.assertEqual(module.module.__name__, 'pkg.module') - def test_module_from_package_triggers_ModuleNotFoundError(self): - # If a submodule causes an ModuleNotFoundError because it tries to import - # a module which doesn't exist, that should let the ModuleNotFoundError + def test_module_from_package_triggers_ImportError(self): + # If a submodule causes an ImportError because it tries to import + # a module which doesn't exist, that should let the ImportError # propagate. def module_code(): import i_do_not_exist with util.mock_modules('pkg.__init__', 'pkg.mod', module_code={'pkg.mod': module_code}) as importer: with util.import_state(meta_path=[importer]): - with self.assertRaises(ModuleNotFoundError) as exc: + with self.assertRaises(ImportError) as exc: import_util.import_('pkg', fromlist=['mod']) self.assertEqual('i_do_not_exist', exc.exception.name) diff --git a/Lib/test/test_pydoc.py b/Lib/test/test_pydoc.py index 95ef115..399f818 100644 --- a/Lib/test/test_pydoc.py +++ b/Lib/test/test_pydoc.py @@ -206,7 +206,7 @@ expected_html_data_docstrings = tuple(s.replace(' ', ' ') missing_pattern = "no Python documentation found for '%s'" # output pattern for module with bad imports -badimport_pattern = "problem in %s - ModuleNotFoundError: No module named %r" +badimport_pattern = "problem in %s - ImportError: No module named %r" def run_pydoc(module_name, *args, **env): """ diff --git a/Lib/test/test_site.py b/Lib/test/test_site.py index 88c2765..4aff932 100644 --- a/Lib/test/test_site.py +++ b/Lib/test/test_site.py @@ -131,7 +131,7 @@ class HelperFunctionsTests(unittest.TestCase): re.escape(os.path.join(pth_dir, pth_fn))) # XXX: ditto previous XXX comment. self.assertRegex(err_out.getvalue(), 'Traceback') - self.assertRegex(err_out.getvalue(), 'ModuleNotFoundError') + self.assertRegex(err_out.getvalue(), 'ImportError') @unittest.skipIf(sys.platform == "win32", "Windows does not raise an " "error for file paths containing null characters") |