Commit 2153dc00 authored by Brett Cannon's avatar Brett Cannon

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.
parent c5951fc9
...@@ -54,13 +54,15 @@ class LoaderTests(abc.LoaderTests): ...@@ -54,13 +54,15 @@ class LoaderTests(abc.LoaderTests):
def test_unloadable(self): def test_unloadable(self):
name = 'dssdsdfff' name = 'dssdsdfff'
assert name not in sys.builtin_module_names 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): def test_already_imported(self):
# Using the name of a module already imported but not a built-in should # Using the name of a module already imported but not a built-in should
# still fail. # still fail.
assert hasattr(importlib, '__file__') # Not a built-in. 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): class InspectLoaderTests(unittest.TestCase):
...@@ -86,7 +88,8 @@ class InspectLoaderTests(unittest.TestCase): ...@@ -86,7 +88,8 @@ class InspectLoaderTests(unittest.TestCase):
# Modules not built-in should raise ImportError. # Modules not built-in should raise ImportError.
for meth_name in ('get_code', 'get_source', 'is_package'): for meth_name in ('get_code', 'get_source', 'is_package'):
method = getattr(machinery.BuiltinImporter, meth_name) method = getattr(machinery.BuiltinImporter, meth_name)
self.assertRaises(ImportError, method, builtin_util.BAD_NAME) with self.assertRaises(ImportError):
method(builtin_util.BAD_NAME)
......
...@@ -46,7 +46,8 @@ class LoaderTests(abc.LoaderTests): ...@@ -46,7 +46,8 @@ class LoaderTests(abc.LoaderTests):
pass pass
def test_unloadable(self): def test_unloadable(self):
self.assertRaises(ImportError, self.load_module, 'asdfjkl;') with self.assertRaises(ImportError):
self.load_module('asdfjkl;')
def test_main(): def test_main():
......
...@@ -51,8 +51,8 @@ class LoaderTests(abc.LoaderTests): ...@@ -51,8 +51,8 @@ class LoaderTests(abc.LoaderTests):
def test_unloadable(self): def test_unloadable(self):
assert machinery.FrozenImporter.find_module('_not_real') is None assert machinery.FrozenImporter.find_module('_not_real') is None
self.assertRaises(ImportError, machinery.FrozenImporter.load_module, with self.assertRaises(ImportError):
'_not_real') machinery.FrozenImporter.load_module('_not_real')
class InspectLoaderTests(unittest.TestCase): class InspectLoaderTests(unittest.TestCase):
...@@ -84,7 +84,8 @@ class InspectLoaderTests(unittest.TestCase): ...@@ -84,7 +84,8 @@ class InspectLoaderTests(unittest.TestCase):
# Raise ImportError for modules that are not frozen. # Raise ImportError for modules that are not frozen.
for meth_name in ('get_code', 'get_source', 'is_package'): for meth_name in ('get_code', 'get_source', 'is_package'):
method = getattr(machinery.FrozenImporter, meth_name) method = getattr(machinery.FrozenImporter, meth_name)
self.assertRaises(ImportError, method, 'importlib') with self.assertRaises(ImportError):
method('importlib')
def test_main(): def test_main():
......
...@@ -18,8 +18,8 @@ class ParentModuleTests(unittest.TestCase): ...@@ -18,8 +18,8 @@ class ParentModuleTests(unittest.TestCase):
def test_bad_parent(self): def test_bad_parent(self):
with util.mock_modules('pkg.module') as mock: with util.mock_modules('pkg.module') as mock:
with util.import_state(meta_path=[mock]): with util.import_state(meta_path=[mock]):
self.assertRaises(ImportError, with self.assertRaises(ImportError):
import_util.import_, 'pkg.module') import_util.import_('pkg.module')
def test_main(): def test_main():
......
...@@ -154,8 +154,9 @@ class RelativeImports(unittest.TestCase): ...@@ -154,8 +154,9 @@ class RelativeImports(unittest.TestCase):
{'__name__': 'pkg', '__path__': ['blah']}) {'__name__': 'pkg', '__path__': ['blah']})
def callback(global_): def callback(global_):
import_util.import_('pkg') import_util.import_('pkg')
self.assertRaises(ValueError, import_util.import_, '', global_, with self.assertRaises(ValueError):
fromlist=['top_level'], level=2) import_util.import_('', global_, fromlist=['top_level'],
level=2)
self.relative_import_test(create, globals_, callback) self.relative_import_test(create, globals_, callback)
def test_too_high_from_module(self): def test_too_high_from_module(self):
...@@ -164,13 +165,15 @@ class RelativeImports(unittest.TestCase): ...@@ -164,13 +165,15 @@ class RelativeImports(unittest.TestCase):
globals_ = {'__package__': 'pkg'}, {'__name__': 'pkg.module'} globals_ = {'__package__': 'pkg'}, {'__name__': 'pkg.module'}
def callback(global_): def callback(global_):
import_util.import_('pkg') import_util.import_('pkg')
self.assertRaises(ValueError, import_util.import_, '', global_, with self.assertRaises(ValueError):
fromlist=['top_level'], level=2) import_util.import_('', global_, fromlist=['top_level'],
level=2)
self.relative_import_test(create, globals_, callback) self.relative_import_test(create, globals_, callback)
def test_empty_name_w_level_0(self): def test_empty_name_w_level_0(self):
# [empty name] # [empty name]
self.assertRaises(ValueError, import_util.import_, '') with self.assertRaises(ValueError):
import_util.import_('')
def test_import_from_different_package(self): def test_import_from_different_package(self):
# Test importing from a different package than the caller. # Test importing from a different package than the caller.
......
...@@ -93,7 +93,8 @@ class SimpleTest(unittest.TestCase): ...@@ -93,7 +93,8 @@ class SimpleTest(unittest.TestCase):
file.write('+++ bad syntax +++') file.write('+++ bad syntax +++')
loader = _bootstrap._PyPycFileLoader('_temp', mapping['_temp'], loader = _bootstrap._PyPycFileLoader('_temp', mapping['_temp'],
False) False)
self.assertRaises(SyntaxError, loader.load_module, name) with self.assertRaises(SyntaxError):
loader.load_module(name)
for attr in attributes: for attr in attributes:
self.assertEqual(getattr(orig_module, attr), value) self.assertEqual(getattr(orig_module, attr), value)
...@@ -104,7 +105,8 @@ class SimpleTest(unittest.TestCase): ...@@ -104,7 +105,8 @@ class SimpleTest(unittest.TestCase):
file.write('=') file.write('=')
loader = _bootstrap._PyPycFileLoader('_temp', mapping['_temp'], loader = _bootstrap._PyPycFileLoader('_temp', mapping['_temp'],
False) False)
self.assertRaises(SyntaxError, loader.load_module, '_temp') with self.assertRaises(SyntaxError):
loader.load_module('_temp')
self.assertTrue('_temp' not in sys.modules) self.assertTrue('_temp' not in sys.modules)
...@@ -166,8 +168,8 @@ class BadBytecodeTest(unittest.TestCase): ...@@ -166,8 +168,8 @@ class BadBytecodeTest(unittest.TestCase):
bytecode_file.write(imp.get_magic()) bytecode_file.write(imp.get_magic())
bytecode_file.write(source_timestamp) bytecode_file.write(source_timestamp)
bytecode_file.write(b'AAAA') bytecode_file.write(b'AAAA')
self.assertRaises(ValueError, self.import_, mapping['_temp'], with self.assertRaises(ValueError):
'_temp') self.import_(mapping['_temp'], '_temp')
self.assertTrue('_temp' not in sys.modules) self.assertTrue('_temp' not in sys.modules)
......
...@@ -97,8 +97,8 @@ class FinderTests(abc.FinderTests): ...@@ -97,8 +97,8 @@ class FinderTests(abc.FinderTests):
with context as mapping: with context as mapping:
os.unlink(mapping['pkg.sub.__init__']) os.unlink(mapping['pkg.sub.__init__'])
pkg_dir = os.path.dirname(mapping['pkg.__init__']) pkg_dir = os.path.dirname(mapping['pkg.__init__'])
self.assertRaises(ImportWarning, self.import_, pkg_dir, with self.assertRaises(ImportWarning):
'pkg.sub') self.import_(pkg_dir, 'pkg.sub')
# [package over modules] # [package over modules]
def test_package_over_module(self): def test_package_over_module(self):
...@@ -117,8 +117,8 @@ class FinderTests(abc.FinderTests): ...@@ -117,8 +117,8 @@ class FinderTests(abc.FinderTests):
def test_empty_dir(self): def test_empty_dir(self):
with warnings.catch_warnings(): with warnings.catch_warnings():
warnings.simplefilter("error", ImportWarning) warnings.simplefilter("error", ImportWarning)
self.assertRaises(ImportWarning, self.run_test, 'pkg', with self.assertRaises(ImportWarning):
{'pkg.__init__'}, unlink={'pkg.__init__'}) self.run_test('pkg', {'pkg.__init__'}, unlink={'pkg.__init__'})
def test_main(): def test_main():
......
...@@ -81,7 +81,8 @@ class EncodingTest(unittest.TestCase): ...@@ -81,7 +81,8 @@ class EncodingTest(unittest.TestCase):
# [BOM conflict] # [BOM conflict]
def test_bom_conflict(self): def test_bom_conflict(self):
source = codecs.BOM_UTF8 + self.create_source('latin-1') 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): class LineEndingTest(unittest.TestCase):
......
...@@ -63,7 +63,8 @@ class ImportModuleTests(unittest.TestCase): ...@@ -63,7 +63,8 @@ class ImportModuleTests(unittest.TestCase):
def test_relative_import_wo_package(self): def test_relative_import_wo_package(self):
# Relative imports cannot happen without the 'package' argument being # Relative imports cannot happen without the 'package' argument being
# set. # set.
self.assertRaises(TypeError, importlib.import_module, '.support') with self.assertRaises(TypeError):
importlib.import_module('.support')
def test_main(): def test_main():
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment