cpython: 9623c83ba489 (original) (raw)
Mercurial > cpython
changeset 77825:9623c83ba489
Changed importlib tests to use assertIs, assertIsInstance, etc., instead of just assertTrue.
line wrap: on
line diff
--- a/Lib/importlib/test/builtin/test_finder.py +++ b/Lib/importlib/test/builtin/test_finder.py @@ -35,14 +35,14 @@ class FinderTests(abc.FinderTests): def test_failure(self): assert 'importlib' not in sys.builtin_module_names loader = machinery.BuiltinImporter.find_module('importlib')
self.assertTrue(loader is None)[](#l1.7)
self.assertIs(loader, None)[](#l1.8)
def test_ignore_path(self): # The value for 'path' should always trigger a failed import. with util.uncache(builtin_util.NAME): loader = machinery.BuiltinImporter.find_module(builtin_util.NAME, ['pkg'])
self.assertTrue(loader is None)[](#l1.15)
self.assertIs(loader, None)[](#l1.16)
--- a/Lib/importlib/test/builtin/test_loader.py +++ b/Lib/importlib/test/builtin/test_loader.py @@ -18,10 +18,10 @@ class LoaderTests(abc.LoaderTests): def verify(self, module): """Verify that the module matches against what it should have."""
self.assertTrue(isinstance(module, types.ModuleType))[](#l2.7)
self.assertIsInstance(module, types.ModuleType)[](#l2.8) for attr, value in self.verification.items():[](#l2.9) self.assertEqual(getattr(module, attr), value)[](#l2.10)
self.assertTrue(module.__name__ in sys.modules)[](#l2.11)
self.assertIn(module.__name__, sys.modules)[](#l2.12)
load_module = staticmethod(lambda name: machinery.BuiltinImporter.load_module(name)) @@ -49,7 +49,7 @@ class LoaderTests(abc.LoaderTests): with util.uncache(builtin_util.NAME): module1 = self.load_module(builtin_util.NAME) module2 = self.load_module(builtin_util.NAME)
self.assertTrue(module1 is module2)[](#l2.20)
self.assertIs(module1, module2)[](#l2.21)
def test_unloadable(self): name = 'dssdsdfff' @@ -74,12 +74,12 @@ class InspectLoaderTests(unittest.TestCa def test_get_code(self): # There is no code object. result = machinery.BuiltinImporter.get_code(builtin_util.NAME)
self.assertTrue(result is None)[](#l2.29)
self.assertIs(result, None)[](#l2.30)
def test_get_source(self): # There is no source. result = machinery.BuiltinImporter.get_source(builtin_util.NAME)
self.assertTrue(result is None)[](#l2.35)
self.assertIs(result, None)[](#l2.36)
def test_is_package(self): # Cannot be a package.
--- a/Lib/importlib/test/extension/test_finder.py +++ b/Lib/importlib/test/extension/test_finder.py @@ -36,7 +36,7 @@ class FinderTests(abc.FinderTests): pass def test_failure(self):
self.assertTrue(self.find_module('asdfjkl;') is None)[](#l3.7)
self.assertIs(self.find_module('asdfjkl;'), None)[](#l3.8)
# XXX Raise an exception if someone tries to use the 'path' argument?
--- a/Lib/importlib/test/extension/test_loader.py +++ b/Lib/importlib/test/extension/test_loader.py @@ -33,9 +33,9 @@ class LoaderTests(abc.LoaderTests): ('file', ext_util.FILEPATH), ('package', '')]: self.assertEqual(getattr(module, attr), value)
self.assertTrue(ext_util.NAME in sys.modules)[](#l4.7)
self.assertTrue(isinstance(module.__loader__,[](#l4.8)
machinery.ExtensionFileLoader))[](#l4.9)
self.assertIn(ext_util.NAME, sys.modules)[](#l4.10)
self.assertIsInstance(module.__loader__,[](#l4.11)
machinery.ExtensionFileLoader)[](#l4.12)
def test_package(self): # Extensions are not found in packages. @@ -49,7 +49,7 @@ class LoaderTests(abc.LoaderTests): with util.uncache(ext_util.NAME): module1 = self.load_module(ext_util.NAME) module2 = self.load_module(ext_util.NAME)
self.assertTrue(module1 is module2)[](#l4.20)
self.assertIs(module1, module2)[](#l4.21)
def test_state_after_failure(self): # No easy way to trigger a failure after a successful import.
--- a/Lib/importlib/test/frozen/test_finder.py +++ b/Lib/importlib/test/frozen/test_finder.py @@ -35,7 +35,7 @@ class FinderTests(abc.FinderTests): def test_failure(self): loader = self.find('')
self.assertTrue(loader is None)[](#l5.7)
self.assertIs(loader, None)[](#l5.8)
--- a/Lib/importlib/test/frozen/test_loader.py +++ b/Lib/importlib/test/frozen/test_loader.py @@ -55,7 +55,7 @@ class LoaderTests(abc.LoaderTests): with util.uncache('hello'), captured_stdout() as stdout: module1 = machinery.FrozenImporter.load_module('hello') module2 = machinery.FrozenImporter.load_module('hello')
self.assertTrue(module1 is module2)[](#l6.7)
self.assertIs(module1, module2)[](#l6.8) self.assertEqual(stdout.getvalue(),[](#l6.9) 'Hello world!\nHello world!\n')[](#l6.10)
@@ -93,7 +93,7 @@ class InspectLoaderTests(unittest.TestCa def test_get_source(self): # Should always return None. result = machinery.FrozenImporter.get_source('hello')
self.assertTrue(result is None)[](#l6.16)
self.assertIs(result, None)[](#l6.17)
def test_is_package(self): # Should be able to tell what is a package. @@ -101,7 +101,7 @@ class InspectLoaderTests(unittest.TestCa ('phello.spam', False)) for name, is_package in test_for: result = machinery.FrozenImporter.is_package(name)
self.assertTrue(bool(result) == is_package)[](#l6.25)
self.assertEqual(bool(result), is_package)[](#l6.26)
def test_failure(self): # Raise ImportError for modules that are not frozen.
--- a/Lib/importlib/test/import_/test_caching.py +++ b/Lib/importlib/test/import_/test_caching.py @@ -65,7 +65,8 @@ class UseCache(unittest.TestCase): with util.import_state(meta_path=[importer]): module = import_util.import_('pkg.module') self.assertTrue(hasattr(module, 'module'))
self.assertTrue(id(module.module), id(sys.modules['pkg.module']))[](#l7.7)
self.assertEqual(id(module.module),[](#l7.8)
id(sys.modules['pkg.module']))[](#l7.9)
# See test_using_cache_after_loader() for reasoning. @import_util.importlib_only
--- a/Lib/importlib/test/import_/test_meta_path.py +++ b/Lib/importlib/test/import_/test_meta_path.py @@ -82,7 +82,7 @@ class CallSignature(unittest.TestCase): self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 0) self.assertEqual(args[0], mod_name)
self.assertTrue(args[1] is None)[](#l8.7)
self.assertIs(args[1], None)[](#l8.8)
def test_with_path(self): # [path set] @@ -102,7 +102,7 @@ class CallSignature(unittest.TestCase): # Assuming all arguments are positional. self.assertTrue(not kwargs) self.assertEqual(args[0], mod_name)
self.assertTrue(args[1] is path)[](#l8.16)
self.assertIs(args[1], path)[](#l8.17)
--- a/Lib/importlib/test/import_/test_packages.py +++ b/Lib/importlib/test/import_/test_packages.py @@ -14,7 +14,7 @@ class ParentModuleTests(unittest.TestCas with util.mock_modules('pkg.init', 'pkg.module') as mock: with util.import_state(meta_path=[mock]): module = import_util.import_('pkg.module')
self.assertTrue('pkg' in sys.modules)[](#l9.7)
self.assertIn('pkg', sys.modules)[](#l9.8)
def test_bad_parent(self): with util.mock_modules('pkg.module') as mock: @@ -33,12 +33,12 @@ class ParentModuleTests(unittest.TestCas with util.import_state(meta_path=[mock]): with self.assertRaises(ZeroDivisionError): import_util.import_('pkg')
self.assertFalse('pkg' in sys.modules)[](#l9.16)
self.assertTrue('pkg.module' in sys.modules)[](#l9.17)
self.assertNotIn('pkg', sys.modules)[](#l9.18)
self.assertIn('pkg.module', sys.modules)[](#l9.19) with self.assertRaises(ZeroDivisionError):[](#l9.20) import_util.import_('pkg.module')[](#l9.21)
self.assertFalse('pkg' in sys.modules)[](#l9.22)
self.assertTrue('pkg.module' in sys.modules)[](#l9.23)
self.assertNotIn('pkg', sys.modules)[](#l9.24)
self.assertIn('pkg.module', sys.modules)[](#l9.25)
def test_raising_parent_after_relative_importing_child(self): def init(): @@ -52,12 +52,12 @@ class ParentModuleTests(unittest.TestCas # This raises ImportError on the "from . import module" # line, not sure why. import_util.import_('pkg')
self.assertFalse('pkg' in sys.modules)[](#l9.33)
self.assertNotIn('pkg', sys.modules)[](#l9.34) with self.assertRaises((ZeroDivisionError, ImportError)):[](#l9.35) import_util.import_('pkg.module')[](#l9.36)
self.assertFalse('pkg' in sys.modules)[](#l9.37)
self.assertNotIn('pkg', sys.modules)[](#l9.38) # XXX False[](#l9.39)
#self.assertTrue('pkg.module' in sys.modules)[](#l9.40)
#self.assertIn('pkg.module', sys.modules)[](#l9.41)
def test_raising_parent_after_double_relative_importing_child(self): def init(): @@ -72,12 +72,12 @@ class ParentModuleTests(unittest.TestCas # This raises ImportError on the "from ..subpkg import module" # line, not sure why. import_util.import_('pkg.subpkg')
self.assertFalse('pkg.subpkg' in sys.modules)[](#l9.49)
self.assertNotIn('pkg.subpkg', sys.modules)[](#l9.50) with self.assertRaises((ZeroDivisionError, ImportError)):[](#l9.51) import_util.import_('pkg.subpkg.module')[](#l9.52)
self.assertFalse('pkg.subpkg' in sys.modules)[](#l9.53)
self.assertNotIn('pkg.subpkg', sys.modules)[](#l9.54) # XXX False[](#l9.55)
#self.assertTrue('pkg.subpkg.module' in sys.modules)[](#l9.56)
#self.assertIn('pkg.subpkg.module', sys.modules)[](#l9.57)
def test_module_not_package(self): # Try to import a submodule from a non-package should raise ImportError.
--- a/Lib/importlib/test/import_/test_path.py +++ b/Lib/importlib/test/import_/test_path.py @@ -20,7 +20,7 @@ class FinderTests(unittest.TestCase): # Test None returned upon not finding a suitable finder. module = '' with util.import_state():
self.assertTrue(machinery.PathFinder.find_module(module) is None)[](#l10.7)
self.assertIs(machinery.PathFinder.find_module(module), None)[](#l10.8)
def test_sys_path(self): # Test that sys.path is used when 'path' is None. @@ -31,7 +31,7 @@ class FinderTests(unittest.TestCase): with util.import_state(path_importer_cache={path: importer}, path=[path]): loader = machinery.PathFinder.find_module(module)
self.assertTrue(loader is importer)[](#l10.16)
self.assertIs(loader, importer)[](#l10.17)
def test_path(self): # Test that 'path' is used when set. @@ -41,7 +41,7 @@ class FinderTests(unittest.TestCase): importer = util.mock_modules(module) with util.import_state(path_importer_cache={path: importer}): loader = machinery.PathFinder.find_module(module, [path])
self.assertTrue(loader is importer)[](#l10.25)
self.assertIs(loader, importer)[](#l10.26)
def test_empty_list(self): # An empty list should not count as asking for sys.path. @@ -61,9 +61,9 @@ class FinderTests(unittest.TestCase): hook = import_util.mock_path_hook(path, importer=importer) with util.import_state(path_hooks=[hook]): loader = machinery.PathFinder.find_module(module, [path])
self.assertTrue(loader is importer)[](#l10.34)
self.assertTrue(path in sys.path_importer_cache)[](#l10.35)
self.assertTrue(sys.path_importer_cache[path] is importer)[](#l10.36)
self.assertIs(loader, importer)[](#l10.37)
self.assertIn(path, sys.path_importer_cache)[](#l10.38)
self.assertIs(sys.path_importer_cache[path], importer)[](#l10.39)
def test_empty_path_hooks(self): # Test that if sys.path_hooks is empty a warning is raised,
--- a/Lib/importlib/test/source/test_abc_loader.py +++ b/Lib/importlib/test/source/test_abc_loader.py @@ -221,7 +221,7 @@ class PyLoaderTests(testing_abc.LoaderTe mock = self.mocker({name: path}) with util.uncache(name): module = mock.load_module(name)
self.assertTrue(name in sys.modules)[](#l11.7)
self.assertIn(name, sys.modules)[](#l11.8) self.eq_attrs(module, __name__=name, __file__=path, __package__='',[](#l11.9) __loader__=mock)[](#l11.10) self.assertTrue(not hasattr(module, '__path__'))[](#l11.11)
@@ -233,7 +233,7 @@ class PyLoaderTests(testing_abc.LoaderTe mock = self.mocker({name: path}) with util.uncache(name): module = mock.load_module(name)
self.assertTrue(name in sys.modules)[](#l11.16)
self.assertIn(name, sys.modules)[](#l11.17) self.eq_attrs(module, __name__=name, __file__=path,[](#l11.18) __path__=[os.path.dirname(path)], __package__=name,[](#l11.19) __loader__=mock)[](#l11.20)
@@ -259,8 +259,8 @@ class PyLoaderTests(testing_abc.LoaderTe with util.uncache(name): sys.modules[name] = module loaded_module = mock.load_module(name)
self.assertTrue(loaded_module is module)[](#l11.25)
self.assertTrue(sys.modules[name] is module)[](#l11.26)
self.assertIs(loaded_module, module)[](#l11.27)
self.assertIs(sys.modules[name], module)[](#l11.28) return mock, name[](#l11.29)
def test_state_after_failure(self): @@ -273,7 +273,7 @@ class PyLoaderTests(testing_abc.LoaderTe sys.modules[name] = module with self.assertRaises(ZeroDivisionError): mock.load_module(name)
self.assertTrue(sys.modules[name] is module)[](#l11.36)
self.assertIs(sys.modules[name], module)[](#l11.37) self.assertTrue(hasattr(module, 'blah'))[](#l11.38) return mock[](#l11.39)
@@ -284,7 +284,7 @@ class PyLoaderTests(testing_abc.LoaderTe with util.uncache(name): with self.assertRaises(ZeroDivisionError): mock.load_module(name)
self.assertTrue(name not in sys.modules)[](#l11.45)
self.assertNotIn(name, sys.modules)[](#l11.46) return mock[](#l11.47)
@@ -414,8 +414,7 @@ class SkipWritingBytecodeTests(unittest. sys.dont_write_bytecode = dont_write_bytecode with util.uncache(name): mock.load_module(name)
self.assertTrue((name in mock.module_bytecode) is not[](#l11.54)
dont_write_bytecode)[](#l11.55)
self.assertIsNot(name in mock.module_bytecode, dont_write_bytecode)[](#l11.56)
def test_no_bytecode_written(self): self.run_test(True) @@ -440,7 +439,7 @@ class RegeneratedBytecodeTests(unittest. 'magic': bad_magic}}) with util.uncache(name): mock.load_module(name)
self.assertTrue(name in mock.module_bytecode)[](#l11.64)
self.assertIn(name, mock.module_bytecode)[](#l11.65) magic = mock.module_bytecode[name][:4][](#l11.66) self.assertEqual(magic, imp.get_magic())[](#l11.67)
@@ -453,7 +452,7 @@ class RegeneratedBytecodeTests(unittest. {name: {'path': 'path/to/mod.bytecode', 'mtime': old_mtime}}) with util.uncache(name): mock.load_module(name)
self.assertTrue(name in mock.module_bytecode)[](#l11.73)
self.assertIn(name, mock.module_bytecode)[](#l11.74) mtime = importlib._r_long(mock.module_bytecode[name][4:8])[](#l11.75) self.assertEqual(mtime, PyPycLoaderMock.default_mtime)[](#l11.76)
@@ -621,7 +620,7 @@ class SourceOnlyLoaderTests(SourceLoader module = self.loader.load_module(self.name) self.verify_module(module) self.assertEqual(module.path, [os.path.dirname(self.path)])
self.assertTrue(self.name in sys.modules)[](#l11.82)
self.assertIn(self.name, sys.modules)[](#l11.83)
def test_package_settings(self): # package needs to be set, while path is set on if the module
--- a/Lib/importlib/test/source/test_file_loader.py +++ b/Lib/importlib/test/source/test_file_loader.py @@ -64,7 +64,7 @@ class SimpleTest(unittest.TestCase): with source_util.create_modules('_temp') as mapping: loader = _bootstrap.SourceFileLoader('_temp', mapping['_temp']) module = loader.load_module('_temp')
self.assertTrue('_temp' in sys.modules)[](#l12.7)
self.assertIn('_temp', sys.modules)[](#l12.8) check = {'__name__': '_temp', '__file__': mapping['_temp'],[](#l12.9) '__package__': ''}[](#l12.10) for attr, value in check.items():[](#l12.11)
@@ -75,7 +75,7 @@ class SimpleTest(unittest.TestCase): loader = _bootstrap.SourceFileLoader('_pkg', mapping['_pkg.init']) module = loader.load_module('_pkg')
self.assertTrue('_pkg' in sys.modules)[](#l12.16)
self.assertIn('_pkg', sys.modules)[](#l12.17) check = {'__name__': '_pkg', '__file__': mapping['_pkg.__init__'],[](#l12.18) '__path__': [os.path.dirname(mapping['_pkg.__init__'])],[](#l12.19) '__package__': '_pkg'}[](#l12.20)
@@ -88,7 +88,7 @@ class SimpleTest(unittest.TestCase): loader = _bootstrap.SourceFileLoader('_pkg.mod', mapping['_pkg.mod']) module = loader.load_module('_pkg.mod')
self.assertTrue('_pkg.mod' in sys.modules)[](#l12.25)
self.assertIn('_pkg.mod', sys.modules)[](#l12.26) check = {'__name__': '_pkg.mod', '__file__': mapping['_pkg.mod'],[](#l12.27) '__package__': '_pkg'}[](#l12.28) for attr, value in check.items():[](#l12.29)
@@ -107,7 +107,7 @@ class SimpleTest(unittest.TestCase): with open(mapping['_temp'], 'w') as file: file.write("testing_var = 42\n") module = loader.load_module('_temp')
self.assertTrue('testing_var' in module.__dict__,[](#l12.34)
self.assertIn('testing_var', module.__dict__,[](#l12.35) "'testing_var' not in "[](#l12.36) "{0}".format(list(module.__dict__.keys())))[](#l12.37) self.assertEqual(module, sys.modules['_temp'])[](#l12.38)
@@ -139,7 +139,7 @@ class SimpleTest(unittest.TestCase): loader = _bootstrap.SourceFileLoader('_temp', mapping['_temp']) with self.assertRaises(SyntaxError): loader.load_module('_temp')
self.assertTrue('_temp' not in sys.modules)[](#l12.43)
self.assertNotIn('_temp', sys.modules)[](#l12.44)
def test_file_from_empty_string_dir(self): # Loading a module found from an empty string entry on sys.path should @@ -189,7 +189,7 @@ class BadBytecodeTest(unittest.TestCase) def import_(self, file, module_name): loader = self.loader(module_name, file) module = loader.load_module(module_name)
self.assertTrue(module_name in sys.modules)[](#l12.52)
self.assertIn(module_name, sys.modules)[](#l12.53)
def manipulate_bytecode(self, name, mapping, manipulator, *, del_source=False):
--- a/Lib/importlib/test/source/test_finder.py +++ b/Lib/importlib/test/source/test_finder.py @@ -115,7 +115,7 @@ class FinderTests(abc.FinderTests): def test_failure(self): with source_util.create_modules('blah') as mapping: nothing = self.import_(mapping['.root'], 'sdfsadsadf')
self.assertTrue(nothing is None)[](#l13.7)
self.assertIs(nothing, None)[](#l13.8)
def test_empty_string_for_dir(self): # The empty string from sys.path means to search in the cwd.
--- a/Lib/importlib/test/test_util.py +++ b/Lib/importlib/test/test_util.py @@ -29,8 +29,8 @@ class ModuleForLoaderTests(unittest.Test module_name = 'a.b.c' with test_util.uncache(module_name): module = self.return_module(module_name)
self.assertTrue(module_name in sys.modules)[](#l14.7)
self.assertTrue(isinstance(module, types.ModuleType))[](#l14.8)
self.assertIn(module_name, sys.modules)[](#l14.9)
self.assertIsInstance(module, types.ModuleType)[](#l14.10) self.assertEqual(module.__name__, module_name)[](#l14.11)
def test_reload(self): @@ -48,7 +48,7 @@ class ModuleForLoaderTests(unittest.Test name = 'a.b.c' with test_util.uncache(name): self.raise_exception(name)
self.assertTrue(name not in sys.modules)[](#l14.18)
self.assertNotIn(name, sys.modules)[](#l14.19)
def test_reload_failure(self): # Test that a failure on reload leaves the module in-place. @@ -77,7 +77,7 @@ class ModuleForLoaderTests(unittest.Test self.assertFalse(module) sys.modules[name] = module given = self.return_module(name)
self.assertTrue(given is module)[](#l14.27)
self.assertIs(given, module)[](#l14.28)
def test_attributes_set(self): # name, loader, and package should be set (when