[Python-checkins] r74561 - in python/branches/py3k/Lib/importlib/test: builtin/test_loader.py extension/test_loader.py frozen/test_loader.py import_/test_packages.py import_/test_relative_imports.py source/test_file_loader.py source/test_finder.py source/test_source_encoding.py test_api.py

brett.cannon python-checkins at python.org
Fri Aug 28 01:49:22 CEST 2009


Author: brett.cannon
Date: Fri Aug 28 01:49:21 2009
New Revision: 74561

Log:
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.


Modified:
   python/branches/py3k/Lib/importlib/test/builtin/test_loader.py
   python/branches/py3k/Lib/importlib/test/extension/test_loader.py
   python/branches/py3k/Lib/importlib/test/frozen/test_loader.py
   python/branches/py3k/Lib/importlib/test/import_/test_packages.py
   python/branches/py3k/Lib/importlib/test/import_/test_relative_imports.py
   python/branches/py3k/Lib/importlib/test/source/test_file_loader.py
   python/branches/py3k/Lib/importlib/test/source/test_finder.py
   python/branches/py3k/Lib/importlib/test/source/test_source_encoding.py
   python/branches/py3k/Lib/importlib/test/test_api.py

Modified: python/branches/py3k/Lib/importlib/test/builtin/test_loader.py
==============================================================================
--- python/branches/py3k/Lib/importlib/test/builtin/test_loader.py	(original)
+++ python/branches/py3k/Lib/importlib/test/builtin/test_loader.py	Fri Aug 28 01:49:21 2009
@@ -54,13 +54,15 @@
     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 @@
         # 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)
 
 
 

Modified: python/branches/py3k/Lib/importlib/test/extension/test_loader.py
==============================================================================
--- python/branches/py3k/Lib/importlib/test/extension/test_loader.py	(original)
+++ python/branches/py3k/Lib/importlib/test/extension/test_loader.py	Fri Aug 28 01:49:21 2009
@@ -46,7 +46,8 @@
         pass
 
     def test_unloadable(self):
-        self.assertRaises(ImportError, self.load_module, 'asdfjkl;')
+        with self.assertRaises(ImportError):
+            self.load_module('asdfjkl;')
 
 
 def test_main():

Modified: python/branches/py3k/Lib/importlib/test/frozen/test_loader.py
==============================================================================
--- python/branches/py3k/Lib/importlib/test/frozen/test_loader.py	(original)
+++ python/branches/py3k/Lib/importlib/test/frozen/test_loader.py	Fri Aug 28 01:49:21 2009
@@ -51,8 +51,8 @@
 
     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 @@
         # 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():

Modified: python/branches/py3k/Lib/importlib/test/import_/test_packages.py
==============================================================================
--- python/branches/py3k/Lib/importlib/test/import_/test_packages.py	(original)
+++ python/branches/py3k/Lib/importlib/test/import_/test_packages.py	Fri Aug 28 01:49:21 2009
@@ -18,8 +18,8 @@
     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():

Modified: python/branches/py3k/Lib/importlib/test/import_/test_relative_imports.py
==============================================================================
--- python/branches/py3k/Lib/importlib/test/import_/test_relative_imports.py	(original)
+++ python/branches/py3k/Lib/importlib/test/import_/test_relative_imports.py	Fri Aug 28 01:49:21 2009
@@ -154,8 +154,9 @@
                     {'__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 @@
         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.

Modified: python/branches/py3k/Lib/importlib/test/source/test_file_loader.py
==============================================================================
--- python/branches/py3k/Lib/importlib/test/source/test_file_loader.py	(original)
+++ python/branches/py3k/Lib/importlib/test/source/test_file_loader.py	Fri Aug 28 01:49:21 2009
@@ -93,7 +93,8 @@
                 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 @@
                 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 @@
                 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)
 
 

Modified: python/branches/py3k/Lib/importlib/test/source/test_finder.py
==============================================================================
--- python/branches/py3k/Lib/importlib/test/source/test_finder.py	(original)
+++ python/branches/py3k/Lib/importlib/test/source/test_finder.py	Fri Aug 28 01:49:21 2009
@@ -97,8 +97,8 @@
             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 @@
     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():

Modified: python/branches/py3k/Lib/importlib/test/source/test_source_encoding.py
==============================================================================
--- python/branches/py3k/Lib/importlib/test/source/test_source_encoding.py	(original)
+++ python/branches/py3k/Lib/importlib/test/source/test_source_encoding.py	Fri Aug 28 01:49:21 2009
@@ -81,7 +81,8 @@
     # [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):

Modified: python/branches/py3k/Lib/importlib/test/test_api.py
==============================================================================
--- python/branches/py3k/Lib/importlib/test/test_api.py	(original)
+++ python/branches/py3k/Lib/importlib/test/test_api.py	Fri Aug 28 01:49:21 2009
@@ -63,7 +63,8 @@
     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():


More information about the Python-checkins mailing list