diff options
author | Eric Snow <ericsnowcurrently@gmail.com> | 2014-01-07 03:49:04 (GMT) |
---|---|---|
committer | Eric Snow <ericsnowcurrently@gmail.com> | 2014-01-07 03:49:04 (GMT) |
commit | 1500d49c22e1a38d186f2dddfa6ba2c5a6cd7d5e (patch) | |
tree | ee32b3fd82c294c8bf257949a22481121d8ef246 /Lib/test/test_importlib/source | |
parent | 02b9f9d6bb596d437ac10d71afac8a4781d18d86 (diff) | |
download | cpython-1500d49c22e1a38d186f2dddfa6ba2c5a6cd7d5e.zip cpython-1500d49c22e1a38d186f2dddfa6ba2c5a6cd7d5e.tar.gz cpython-1500d49c22e1a38d186f2dddfa6ba2c5a6cd7d5e.tar.bz2 |
Issue 19713: Add PEP 451-related deprecations.
Diffstat (limited to 'Lib/test/test_importlib/source')
-rw-r--r-- | Lib/test/test_importlib/source/test_file_loader.py | 54 | ||||
-rw-r--r-- | Lib/test/test_importlib/source/test_finder.py | 49 | ||||
-rw-r--r-- | Lib/test/test_importlib/source/test_source_encoding.py | 21 |
3 files changed, 93 insertions, 31 deletions
diff --git a/Lib/test/test_importlib/source/test_file_loader.py b/Lib/test/test_importlib/source/test_file_loader.py index 16e4df2..25a3dae 100644 --- a/Lib/test/test_importlib/source/test_file_loader.py +++ b/Lib/test/test_importlib/source/test_file_loader.py @@ -16,6 +16,7 @@ import stat import sys import types import unittest +import warnings from test.support import make_legacy_pyc, unload @@ -39,7 +40,9 @@ class SimpleTest(abc.LoaderTests): loader = Tester('blah', 'blah.py') self.addCleanup(unload, 'blah') - module = loader.load_module() # Should not raise an exception. + with warnings.catch_warnings(): + warnings.simplefilter('ignore', DeprecationWarning) + module = loader.load_module() # Should not raise an exception. def test_get_filename_API(self): # If fullname is not set then assume self.path is desired. @@ -70,7 +73,9 @@ class SimpleTest(abc.LoaderTests): def test_module(self): with source_util.create_modules('_temp') as mapping: loader = self.machinery.SourceFileLoader('_temp', mapping['_temp']) - module = loader.load_module('_temp') + with warnings.catch_warnings(): + warnings.simplefilter('ignore', DeprecationWarning) + module = loader.load_module('_temp') self.assertIn('_temp', sys.modules) check = {'__name__': '_temp', '__file__': mapping['_temp'], '__package__': ''} @@ -81,7 +86,9 @@ class SimpleTest(abc.LoaderTests): with source_util.create_modules('_pkg.__init__') as mapping: loader = self.machinery.SourceFileLoader('_pkg', mapping['_pkg.__init__']) - module = loader.load_module('_pkg') + with warnings.catch_warnings(): + warnings.simplefilter('ignore', DeprecationWarning) + module = loader.load_module('_pkg') self.assertIn('_pkg', sys.modules) check = {'__name__': '_pkg', '__file__': mapping['_pkg.__init__'], '__path__': [os.path.dirname(mapping['_pkg.__init__'])], @@ -94,7 +101,9 @@ class SimpleTest(abc.LoaderTests): with source_util.create_modules('_pkg.__init__', '_pkg.mod')as mapping: loader = self.machinery.SourceFileLoader('_pkg.mod', mapping['_pkg.mod']) - module = loader.load_module('_pkg.mod') + with warnings.catch_warnings(): + warnings.simplefilter('ignore', DeprecationWarning) + module = loader.load_module('_pkg.mod') self.assertIn('_pkg.mod', sys.modules) check = {'__name__': '_pkg.mod', '__file__': mapping['_pkg.mod'], '__package__': '_pkg'} @@ -108,12 +117,16 @@ class SimpleTest(abc.LoaderTests): def test_module_reuse(self): with source_util.create_modules('_temp') as mapping: loader = self.machinery.SourceFileLoader('_temp', mapping['_temp']) - module = loader.load_module('_temp') + with warnings.catch_warnings(): + warnings.simplefilter('ignore', DeprecationWarning) + module = loader.load_module('_temp') module_id = id(module) module_dict_id = id(module.__dict__) with open(mapping['_temp'], 'w') as file: file.write("testing_var = 42\n") - module = loader.load_module('_temp') + with warnings.catch_warnings(): + warnings.simplefilter('ignore', DeprecationWarning) + module = loader.load_module('_temp') self.assertIn('testing_var', module.__dict__, "'testing_var' not in " "{0}".format(list(module.__dict__.keys()))) @@ -138,7 +151,9 @@ class SimpleTest(abc.LoaderTests): for attr in attributes: self.assertEqual(getattr(orig_module, attr), value) with self.assertRaises(SyntaxError): - loader.load_module(name) + with warnings.catch_warnings(): + warnings.simplefilter('ignore', DeprecationWarning) + loader.load_module(name) for attr in attributes: self.assertEqual(getattr(orig_module, attr), value) @@ -149,7 +164,9 @@ class SimpleTest(abc.LoaderTests): file.write('=') loader = self.machinery.SourceFileLoader('_temp', mapping['_temp']) with self.assertRaises(SyntaxError): - loader.load_module('_temp') + with warnings.catch_warnings(): + warnings.simplefilter('ignore', DeprecationWarning) + loader.load_module('_temp') self.assertNotIn('_temp', sys.modules) def test_file_from_empty_string_dir(self): @@ -161,7 +178,9 @@ class SimpleTest(abc.LoaderTests): try: with util.uncache('_temp'): loader = self.machinery.SourceFileLoader('_temp', file_path) - mod = loader.load_module('_temp') + with warnings.catch_warnings(): + warnings.simplefilter('ignore', DeprecationWarning) + mod = loader.load_module('_temp') self.assertEqual(file_path, mod.__file__) self.assertEqual(self.util.cache_from_source(file_path), mod.__cached__) @@ -196,7 +215,9 @@ class SimpleTest(abc.LoaderTests): self.assertTrue(os.path.exists(compiled)) os.unlink(compiled) # PEP 302 - mod = loader.load_module('_temp') # XXX + with warnings.catch_warnings(): + warnings.simplefilter('ignore', DeprecationWarning) + mod = loader.load_module('_temp') # XXX # Sanity checks. self.assertEqual(mod.__cached__, compiled) self.assertEqual(mod.x, 5) @@ -210,7 +231,9 @@ class SimpleTest(abc.LoaderTests): with self.assertRaises(ImportError): loader.exec_module(module) with self.assertRaises(ImportError): - loader.load_module('bad name') + with warnings.catch_warnings(): + warnings.simplefilter('ignore', DeprecationWarning) + loader.load_module('bad name') Frozen_SimpleTest, Source_SimpleTest = util.test_both( SimpleTest, importlib=importlib, machinery=machinery, abc=importlib_abc, @@ -221,7 +244,10 @@ class BadBytecodeTest: def import_(self, file, module_name): loader = self.loader(module_name, file) - module = loader.load_module(module_name) + with warnings.catch_warnings(): + warnings.simplefilter('ignore', DeprecationWarning) + # XXX Change to use exec_module(). + module = loader.load_module(module_name) self.assertIn(module_name, sys.modules) def manipulate_bytecode(self, name, mapping, manipulator, *, @@ -332,7 +358,9 @@ class BadBytecodeTestPEP302(BadBytecodeTest): def import_(self, file, module_name): loader = self.loader(module_name, file) - module = loader.load_module(module_name) + with warnings.catch_warnings(): + warnings.simplefilter('ignore', DeprecationWarning) + module = loader.load_module(module_name) self.assertIn(module_name, sys.modules) diff --git a/Lib/test/test_importlib/source/test_finder.py b/Lib/test/test_importlib/source/test_finder.py index 36fc3dd..473297b 100644 --- a/Lib/test/test_importlib/source/test_finder.py +++ b/Lib/test/test_importlib/source/test_finder.py @@ -46,6 +46,10 @@ class FinderTests(abc.FinderTests): self.machinery.BYTECODE_SUFFIXES)] return self.machinery.FileFinder(root, *loader_details) + def import_(self, root, module): + finder = self.get_finder(root) + return self._find(finder, module, loader_only=True) + def run_test(self, test, create=None, *, compile_=None, unlink=None): """Test the finding of 'test' with the creation of modules listed in 'create'. @@ -127,7 +131,7 @@ class FinderTests(abc.FinderTests): with open('mod.py', 'w') as file: file.write("# test file for importlib") try: - loader = finder.find_module('mod') + loader = self._find(finder, 'mod', loader_only=True) self.assertTrue(hasattr(loader, 'load_module')) finally: os.unlink('mod.py') @@ -145,8 +149,10 @@ class FinderTests(abc.FinderTests): mod = 'mod' with source_util.create_modules(mod) as mapping: finder = self.get_finder(mapping['.root']) - self.assertIsNotNone(finder.find_module(mod)) - self.assertIsNone(finder.find_module(mod)) + found = self._find(finder, 'mod', loader_only=True) + self.assertIsNotNone(found) + found = self._find(finder, 'mod', loader_only=True) + self.assertIsNone(found) @unittest.skipUnless(sys.platform != 'win32', 'os.chmod() does not support the needed arguments under Windows') @@ -170,29 +176,52 @@ class FinderTests(abc.FinderTests): self.addCleanup(cleanup, tempdir) os.chmod(tempdir.name, stat.S_IWUSR | stat.S_IXUSR) finder = self.get_finder(tempdir.name) - self.assertEqual((None, []), finder.find_loader('doesnotexist')) + found = self._find(finder, 'doesnotexist') + self.assertEqual(found, self.NOT_FOUND) def test_ignore_file(self): # If a directory got changed to a file from underneath us, then don't # worry about looking for submodules. with tempfile.NamedTemporaryFile() as file_obj: finder = self.get_finder(file_obj.name) - self.assertEqual((None, []), finder.find_loader('doesnotexist')) + found = self._find(finder, 'doesnotexist') + self.assertEqual(found, self.NOT_FOUND) + class FinderTestsPEP451(FinderTests): - def import_(self, root, module): - found = self.get_finder(root).find_spec(module) - return found.loader if found is not None else found + NOT_FOUND = None + + def _find(self, finder, name, loader_only=False): + spec = finder.find_spec(name) + return spec.loader if spec is not None else spec Frozen_FinderTestsPEP451, Source_FinderTestsPEP451 = util.test_both( FinderTestsPEP451, machinery=machinery) +class FinderTestsPEP420(FinderTests): + + NOT_FOUND = (None, []) + + def _find(self, finder, name, loader_only=False): + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + loader_portions = finder.find_loader(name) + return loader_portions[0] if loader_only else loader_portions + +Frozen_FinderTestsPEP420, Source_FinderTestsPEP420 = util.test_both( + FinderTestsPEP420, machinery=machinery) + + class FinderTestsPEP302(FinderTests): - def import_(self, root, module): - return self.get_finder(root).find_module(module) + NOT_FOUND = None + + def _find(self, finder, name, loader_only=False): + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + return finder.find_module(name) Frozen_FinderTestsPEP302, Source_FinderTestsPEP302 = util.test_both( FinderTestsPEP302, machinery=machinery) diff --git a/Lib/test/test_importlib/source/test_source_encoding.py b/Lib/test/test_importlib/source/test_source_encoding.py index aaf0041..c62dfa1 100644 --- a/Lib/test/test_importlib/source/test_source_encoding.py +++ b/Lib/test/test_importlib/source/test_source_encoding.py @@ -12,6 +12,7 @@ import types # imported for the parser to use. import unicodedata import unittest +import warnings CODING_RE = re.compile(r'^[ \t\f]*#.*coding[:=][ \t]*([-\w.]+)', re.ASCII) @@ -102,7 +103,9 @@ Frozen_EncodingTestPEP451, Source_EncodingTestPEP451 = util.test_both( class EncodingTestPEP302(EncodingTest): def load(self, loader): - return loader.load_module(self.module_name) + with warnings.catch_warnings(): + warnings.simplefilter('ignore', DeprecationWarning) + return loader.load_module(self.module_name) Frozen_EncodingTestPEP302, Source_EncodingTestPEP302 = util.test_both( EncodingTestPEP302, machinery=machinery) @@ -121,8 +124,8 @@ class LineEndingTest: with open(mapping[module_name], 'wb') as file: file.write(source) loader = self.machinery.SourceFileLoader(module_name, - mapping[module_name]) - return loader.load_module(module_name) + mapping[module_name]) + return self.load(loader, module_name) # [cr] def test_cr(self): @@ -138,9 +141,9 @@ class LineEndingTest: class LineEndingTestPEP451(LineEndingTest): - def load(self, loader): - module = types.ModuleType(self.module_name) - module.__spec__ = importlib.util.spec_from_loader(self.module_name, loader) + def load(self, loader, module_name): + module = types.ModuleType(module_name) + module.__spec__ = importlib.util.spec_from_loader(module_name, loader) loader.exec_module(module) return module @@ -149,8 +152,10 @@ Frozen_LineEndingTestPEP451, Source_LineEndingTestPEP451 = util.test_both( class LineEndingTestPEP302(LineEndingTest): - def load(self, loader): - return loader.load_module(self.module_name) + def load(self, loader, module_name): + with warnings.catch_warnings(): + warnings.simplefilter('ignore', DeprecationWarning) + return loader.load_module(module_name) Frozen_LineEndingTestPEP302, Source_LineEndingTestPEP302 = util.test_both( LineEndingTestPEP302, machinery=machinery) |