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.
This commit is contained in:
Brett Cannon 2009-08-27 23:49:21 +00:00
parent c5951fc996
commit 2153dc001f
9 changed files with 36 additions and 24 deletions

View File

@ -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)

View File

@ -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():

View File

@ -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():

View File

@ -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():

View File

@ -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.

View File

@ -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)

View File

@ -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():

View File

@ -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):

View File

@ -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():