From 2153dc001f5565592b44808d7f0f25815010eb9d Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Thu, 27 Aug 2009 23:49:21 +0000 Subject: Move over to using assertRaises as a context manager for importlib tests. Obviously one shouldn't do whole sale conversions like this, but I was already going through the test code and I was bored at the airport. --- Lib/importlib/test/builtin/test_loader.py | 9 ++++++--- Lib/importlib/test/extension/test_loader.py | 3 ++- Lib/importlib/test/frozen/test_loader.py | 7 ++++--- Lib/importlib/test/import_/test_packages.py | 4 ++-- Lib/importlib/test/import_/test_relative_imports.py | 13 ++++++++----- Lib/importlib/test/source/test_file_loader.py | 10 ++++++---- Lib/importlib/test/source/test_finder.py | 8 ++++---- Lib/importlib/test/source/test_source_encoding.py | 3 ++- Lib/importlib/test/test_api.py | 3 ++- 9 files changed, 36 insertions(+), 24 deletions(-) diff --git a/Lib/importlib/test/builtin/test_loader.py b/Lib/importlib/test/builtin/test_loader.py index dff00ce..1a8539b 100644 --- a/Lib/importlib/test/builtin/test_loader.py +++ b/Lib/importlib/test/builtin/test_loader.py @@ -54,13 +54,15 @@ class LoaderTests(abc.LoaderTests): def test_unloadable(self): name = 'dssdsdfff' assert name not in sys.builtin_module_names - self.assertRaises(ImportError, self.load_module, name) + with self.assertRaises(ImportError): + self.load_module(name) def test_already_imported(self): # Using the name of a module already imported but not a built-in should # still fail. assert hasattr(importlib, '__file__') # Not a built-in. - self.assertRaises(ImportError, self.load_module, 'importlib') + with self.assertRaises(ImportError): + self.load_module('importlib') class InspectLoaderTests(unittest.TestCase): @@ -86,7 +88,8 @@ class InspectLoaderTests(unittest.TestCase): # Modules not built-in should raise ImportError. for meth_name in ('get_code', 'get_source', 'is_package'): method = getattr(machinery.BuiltinImporter, meth_name) - self.assertRaises(ImportError, method, builtin_util.BAD_NAME) + with self.assertRaises(ImportError): + method(builtin_util.BAD_NAME) diff --git a/Lib/importlib/test/extension/test_loader.py b/Lib/importlib/test/extension/test_loader.py index 71841c6..7f6eda3 100644 --- a/Lib/importlib/test/extension/test_loader.py +++ b/Lib/importlib/test/extension/test_loader.py @@ -46,7 +46,8 @@ class LoaderTests(abc.LoaderTests): pass def test_unloadable(self): - self.assertRaises(ImportError, self.load_module, 'asdfjkl;') + with self.assertRaises(ImportError): + self.load_module('asdfjkl;') def test_main(): diff --git a/Lib/importlib/test/frozen/test_loader.py b/Lib/importlib/test/frozen/test_loader.py index fa64f30..c05e22c 100644 --- a/Lib/importlib/test/frozen/test_loader.py +++ b/Lib/importlib/test/frozen/test_loader.py @@ -51,8 +51,8 @@ class LoaderTests(abc.LoaderTests): def test_unloadable(self): assert machinery.FrozenImporter.find_module('_not_real') is None - self.assertRaises(ImportError, machinery.FrozenImporter.load_module, - '_not_real') + with self.assertRaises(ImportError): + machinery.FrozenImporter.load_module('_not_real') class InspectLoaderTests(unittest.TestCase): @@ -84,7 +84,8 @@ class InspectLoaderTests(unittest.TestCase): # Raise ImportError for modules that are not frozen. for meth_name in ('get_code', 'get_source', 'is_package'): method = getattr(machinery.FrozenImporter, meth_name) - self.assertRaises(ImportError, method, 'importlib') + with self.assertRaises(ImportError): + method('importlib') def test_main(): diff --git a/Lib/importlib/test/import_/test_packages.py b/Lib/importlib/test/import_/test_packages.py index b41c36f..1a15ebb 100644 --- a/Lib/importlib/test/import_/test_packages.py +++ b/Lib/importlib/test/import_/test_packages.py @@ -18,8 +18,8 @@ class ParentModuleTests(unittest.TestCase): def test_bad_parent(self): with util.mock_modules('pkg.module') as mock: with util.import_state(meta_path=[mock]): - self.assertRaises(ImportError, - import_util.import_, 'pkg.module') + with self.assertRaises(ImportError): + import_util.import_('pkg.module') def test_main(): diff --git a/Lib/importlib/test/import_/test_relative_imports.py b/Lib/importlib/test/import_/test_relative_imports.py index 5547d4c..a0f6b2d 100644 --- a/Lib/importlib/test/import_/test_relative_imports.py +++ b/Lib/importlib/test/import_/test_relative_imports.py @@ -154,8 +154,9 @@ class RelativeImports(unittest.TestCase): {'__name__': 'pkg', '__path__': ['blah']}) def callback(global_): import_util.import_('pkg') - self.assertRaises(ValueError, import_util.import_, '', global_, - fromlist=['top_level'], level=2) + with self.assertRaises(ValueError): + import_util.import_('', global_, fromlist=['top_level'], + level=2) self.relative_import_test(create, globals_, callback) def test_too_high_from_module(self): @@ -164,13 +165,15 @@ class RelativeImports(unittest.TestCase): globals_ = {'__package__': 'pkg'}, {'__name__': 'pkg.module'} def callback(global_): import_util.import_('pkg') - self.assertRaises(ValueError, import_util.import_, '', global_, - fromlist=['top_level'], level=2) + with self.assertRaises(ValueError): + import_util.import_('', global_, fromlist=['top_level'], + level=2) self.relative_import_test(create, globals_, callback) def test_empty_name_w_level_0(self): # [empty name] - self.assertRaises(ValueError, import_util.import_, '') + with self.assertRaises(ValueError): + import_util.import_('') def test_import_from_different_package(self): # Test importing from a different package than the caller. diff --git a/Lib/importlib/test/source/test_file_loader.py b/Lib/importlib/test/source/test_file_loader.py index 0384e7d..b18750c 100644 --- a/Lib/importlib/test/source/test_file_loader.py +++ b/Lib/importlib/test/source/test_file_loader.py @@ -93,7 +93,8 @@ class SimpleTest(unittest.TestCase): file.write('+++ bad syntax +++') loader = _bootstrap._PyPycFileLoader('_temp', mapping['_temp'], False) - self.assertRaises(SyntaxError, loader.load_module, name) + with self.assertRaises(SyntaxError): + loader.load_module(name) for attr in attributes: self.assertEqual(getattr(orig_module, attr), value) @@ -104,7 +105,8 @@ class SimpleTest(unittest.TestCase): file.write('=') loader = _bootstrap._PyPycFileLoader('_temp', mapping['_temp'], False) - self.assertRaises(SyntaxError, loader.load_module, '_temp') + with self.assertRaises(SyntaxError): + loader.load_module('_temp') self.assertTrue('_temp' not in sys.modules) @@ -166,8 +168,8 @@ class BadBytecodeTest(unittest.TestCase): bytecode_file.write(imp.get_magic()) bytecode_file.write(source_timestamp) bytecode_file.write(b'AAAA') - self.assertRaises(ValueError, self.import_, mapping['_temp'], - '_temp') + with self.assertRaises(ValueError): + self.import_(mapping['_temp'], '_temp') self.assertTrue('_temp' not in sys.modules) diff --git a/Lib/importlib/test/source/test_finder.py b/Lib/importlib/test/source/test_finder.py index c495c5a..8f15f62 100644 --- a/Lib/importlib/test/source/test_finder.py +++ b/Lib/importlib/test/source/test_finder.py @@ -97,8 +97,8 @@ class FinderTests(abc.FinderTests): with context as mapping: os.unlink(mapping['pkg.sub.__init__']) pkg_dir = os.path.dirname(mapping['pkg.__init__']) - self.assertRaises(ImportWarning, self.import_, pkg_dir, - 'pkg.sub') + with self.assertRaises(ImportWarning): + self.import_(pkg_dir, 'pkg.sub') # [package over modules] def test_package_over_module(self): @@ -117,8 +117,8 @@ class FinderTests(abc.FinderTests): def test_empty_dir(self): with warnings.catch_warnings(): warnings.simplefilter("error", ImportWarning) - self.assertRaises(ImportWarning, self.run_test, 'pkg', - {'pkg.__init__'}, unlink={'pkg.__init__'}) + with self.assertRaises(ImportWarning): + self.run_test('pkg', {'pkg.__init__'}, unlink={'pkg.__init__'}) def test_main(): diff --git a/Lib/importlib/test/source/test_source_encoding.py b/Lib/importlib/test/source/test_source_encoding.py index 3734712..fde355f 100644 --- a/Lib/importlib/test/source/test_source_encoding.py +++ b/Lib/importlib/test/source/test_source_encoding.py @@ -81,7 +81,8 @@ class EncodingTest(unittest.TestCase): # [BOM conflict] def test_bom_conflict(self): source = codecs.BOM_UTF8 + self.create_source('latin-1') - self.assertRaises(SyntaxError, self.run_test, source) + with self.assertRaises(SyntaxError): + self.run_test(source) class LineEndingTest(unittest.TestCase): diff --git a/Lib/importlib/test/test_api.py b/Lib/importlib/test/test_api.py index 65f8d04..e729013 100644 --- a/Lib/importlib/test/test_api.py +++ b/Lib/importlib/test/test_api.py @@ -63,7 +63,8 @@ class ImportModuleTests(unittest.TestCase): def test_relative_import_wo_package(self): # Relative imports cannot happen without the 'package' argument being # set. - self.assertRaises(TypeError, importlib.import_module, '.support') + with self.assertRaises(TypeError): + importlib.import_module('.support') def test_main(): -- cgit v0.12