diff options
| author | Brett Cannon <brett@python.org> | 2013-12-06 14:25:01 -0500 | 
|---|---|---|
| committer | Brett Cannon <brett@python.org> | 2013-12-06 14:25:01 -0500 | 
| commit | 2f8c83568ca5850601e92e315c4a1c840e94b1cb (patch) | |
| tree | f76aeedc3eae53677631468f3569eb136fb73b0b | |
| parent | 86aae6a7b3b863529b6cce64859c0f56c7963bc0 (diff) | |
| download | cpython-git-2f8c83568ca5850601e92e315c4a1c840e94b1cb.tar.gz | |
Issue #19712: Update test.test_importlib.source for PEP 451
| -rw-r--r-- | Lib/test/test_importlib/source/test_case_sensitivity.py | 31 | ||||
| -rw-r--r-- | Lib/test/test_importlib/source/test_file_loader.py | 71 | ||||
| -rw-r--r-- | Lib/test/test_importlib/source/test_finder.py | 21 | ||||
| -rw-r--r-- | Lib/test/test_importlib/source/test_source_encoding.py | 41 | 
4 files changed, 140 insertions, 24 deletions
diff --git a/Lib/test/test_importlib/source/test_case_sensitivity.py b/Lib/test/test_importlib/source/test_case_sensitivity.py index b3e9d25bb2..efd3146d90 100644 --- a/Lib/test/test_importlib/source/test_case_sensitivity.py +++ b/Lib/test/test_importlib/source/test_case_sensitivity.py @@ -21,13 +21,12 @@ class CaseSensitivityTest:      name = 'MoDuLe'      assert name != name.lower() -    def find(self, path): -        finder = self.machinery.FileFinder(path, +    def finder(self, path): +        return self.machinery.FileFinder(path,                                        (self.machinery.SourceFileLoader,                                              self.machinery.SOURCE_SUFFIXES),                                          (self.machinery.SourcelessFileLoader,                                              self.machinery.BYTECODE_SUFFIXES)) -        return finder.find_module(self.name)      def sensitivity_test(self):          """Look for a module with matching and non-matching sensitivity.""" @@ -37,7 +36,9 @@ class CaseSensitivityTest:          with context as mapping:              sensitive_path = os.path.join(mapping['.root'], 'sensitive')              insensitive_path = os.path.join(mapping['.root'], 'insensitive') -            return self.find(sensitive_path), self.find(insensitive_path) +            sensitive_finder = self.finder(sensitive_path) +            insensitive_finder = self.finder(insensitive_path) +            return self.find(sensitive_finder), self.find(insensitive_finder)      def test_sensitive(self):          with test_support.EnvironmentVarGuard() as env: @@ -46,7 +47,7 @@ class CaseSensitivityTest:                  self.skipTest('os.environ changes not reflected in '                                '_os.environ')              sensitive, insensitive = self.sensitivity_test() -            self.assertTrue(hasattr(sensitive, 'load_module')) +            self.assertIsNotNone(sensitive)              self.assertIn(self.name, sensitive.get_filename(self.name))              self.assertIsNone(insensitive) @@ -57,13 +58,25 @@ class CaseSensitivityTest:                  self.skipTest('os.environ changes not reflected in '                                '_os.environ')              sensitive, insensitive = self.sensitivity_test() -            self.assertTrue(hasattr(sensitive, 'load_module')) +            self.assertIsNotNone(sensitive)              self.assertIn(self.name, sensitive.get_filename(self.name)) -            self.assertTrue(hasattr(insensitive, 'load_module')) +            self.assertIsNotNone(insensitive)              self.assertIn(self.name, insensitive.get_filename(self.name)) -Frozen_CaseSensitivityTest, Source_CaseSensitivityTest = util.test_both( -    CaseSensitivityTest, importlib=importlib, machinery=machinery) +class CaseSensitivityTestPEP302(CaseSensitivityTest): +    def find(self, finder): +        return finder.find_module(self.name) + +Frozen_CaseSensitivityTestPEP302, Source_CaseSensitivityTestPEP302 = util.test_both( +    CaseSensitivityTestPEP302, importlib=importlib, machinery=machinery) + +class CaseSensitivityTestPEP451(CaseSensitivityTest): +    def find(self, finder): +        found = finder.find_spec(self.name) +        return found.loader if found is not None else found + +Frozen_CaseSensitivityTestPEP451, Source_CaseSensitivityTestPEP451 = util.test_both( +    CaseSensitivityTestPEP451, importlib=importlib, machinery=machinery)  if __name__ == '__main__': diff --git a/Lib/test/test_importlib/source/test_file_loader.py b/Lib/test/test_importlib/source/test_file_loader.py index f1e2713a97..97cbf0621c 100644 --- a/Lib/test/test_importlib/source/test_file_loader.py +++ b/Lib/test/test_importlib/source/test_file_loader.py @@ -121,6 +121,10 @@ class SimpleTest(abc.LoaderTests):                  file.write('+++ bad syntax +++')              loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])              with self.assertRaises(SyntaxError): +                loader.exec_module(orig_module) +            for attr in attributes: +                self.assertEqual(getattr(orig_module, attr), value) +            with self.assertRaises(SyntaxError):                  loader.load_module(name)              for attr in attributes:                  self.assertEqual(getattr(orig_module, attr), value) @@ -171,15 +175,27 @@ class SimpleTest(abc.LoaderTests):                      raise                  self.skipTest("cannot set modification time to large integer ({})".format(e))              loader = self.machinery.SourceFileLoader('_temp', mapping['_temp']) -            mod = loader.load_module('_temp') +            # PEP 451 +            module = types.ModuleType('_temp') +            module.__spec__ = self.util.spec_from_loader('_temp', loader) +            loader.exec_module(module) +            self.assertEqual(module.x, 5) +            self.assertTrue(os.path.exists(compiled)) +            os.unlink(compiled) +            # PEP 302 +            mod = loader.load_module('_temp') # XXX              # Sanity checks.              self.assertEqual(mod.__cached__, compiled)              self.assertEqual(mod.x, 5)              # The pyc file was created. -            os.stat(compiled) +            self.assertTrue(os.path.exists(compiled))      def test_unloadable(self):          loader = self.machinery.SourceFileLoader('good name', {}) +        module = types.ModuleType('bad name') +        module.__spec__ = self.machinery.ModuleSpec('bad name', loader) +        with self.assertRaises(ImportError): +            loader.exec_module(module)          with self.assertRaises(ImportError):              loader.load_module('bad name') @@ -291,8 +307,23 @@ class BadBytecodeTest:                                      lambda bc: b'\x00\x00\x00\x00' + bc[4:])              test('_temp', mapping, bc_path) +class BadBytecodeTestPEP451(BadBytecodeTest): + +    def import_(self, file, module_name): +        loader = self.loader(module_name, file) +        module = types.ModuleType(module_name) +        module.__spec__ = self.util.spec_from_loader(module_name, loader) +        loader.exec_module(module) + +class BadBytecodeTestPEP302(BadBytecodeTest): + +    def import_(self, file, module_name): +        loader = self.loader(module_name, file) +        module = loader.load_module(module_name) +        self.assertIn(module_name, sys.modules) + -class SourceLoaderBadBytecodeTest(BadBytecodeTest): +class SourceLoaderBadBytecodeTest:      @classmethod      def setUpClass(cls): @@ -418,12 +449,24 @@ class SourceLoaderBadBytecodeTest(BadBytecodeTest):                  # Make writable for eventual clean-up.                  os.chmod(bytecode_path, stat.S_IWUSR) -Frozen_SourceBadBytecode, Source_SourceBadBytecode = util.test_both( -        SourceLoaderBadBytecodeTest, importlib=importlib, machinery=machinery, +class SourceLoaderBadBytecodeTestPEP451( +        SourceLoaderBadBytecodeTest, BadBytecodeTestPEP451): +    pass + +Frozen_SourceBadBytecodePEP451, Source_SourceBadBytecodePEP451 = util.test_both( +        SourceLoaderBadBytecodeTestPEP451, importlib=importlib, machinery=machinery,          abc=importlib_abc, util=importlib_util) +class SourceLoaderBadBytecodeTestPEP302( +        SourceLoaderBadBytecodeTest, BadBytecodeTestPEP302): +    pass + +Frozen_SourceBadBytecodePEP302, Source_SourceBadBytecodePEP302 = util.test_both( +        SourceLoaderBadBytecodeTestPEP302, importlib=importlib, machinery=machinery, +        abc=importlib_abc, util=importlib_util) -class SourcelessLoaderBadBytecodeTest(BadBytecodeTest): + +class SourcelessLoaderBadBytecodeTest:      @classmethod      def setUpClass(cls): @@ -482,8 +525,20 @@ class SourcelessLoaderBadBytecodeTest(BadBytecodeTest):      def test_non_code_marshal(self):          self._test_non_code_marshal(del_source=True) -Frozen_SourcelessBadBytecode, Source_SourcelessBadBytecode = util.test_both( -        SourcelessLoaderBadBytecodeTest, importlib=importlib, +class SourcelessLoaderBadBytecodeTestPEP451(SourcelessLoaderBadBytecodeTest, +        BadBytecodeTestPEP451): +    pass + +Frozen_SourcelessBadBytecodePEP451, Source_SourcelessBadBytecodePEP451 = util.test_both( +        SourcelessLoaderBadBytecodeTestPEP451, importlib=importlib, +        machinery=machinery, abc=importlib_abc, util=importlib_util) + +class SourcelessLoaderBadBytecodeTestPEP302(SourcelessLoaderBadBytecodeTest, +        BadBytecodeTestPEP302): +    pass + +Frozen_SourcelessBadBytecodePEP302, Source_SourcelessBadBytecodePEP302 = util.test_both( +        SourcelessLoaderBadBytecodeTestPEP302, importlib=importlib,          machinery=machinery, abc=importlib_abc, util=importlib_util) diff --git a/Lib/test/test_importlib/source/test_finder.py b/Lib/test/test_importlib/source/test_finder.py index 8bf8cb7880..36fc3ddf69 100644 --- a/Lib/test/test_importlib/source/test_finder.py +++ b/Lib/test/test_importlib/source/test_finder.py @@ -46,9 +46,6 @@ class FinderTests(abc.FinderTests):                              self.machinery.BYTECODE_SUFFIXES)]          return self.machinery.FileFinder(root, *loader_details) -    def import_(self, root, module): -        return self.get_finder(root).find_module(module) -      def run_test(self, test, create=None, *, compile_=None, unlink=None):          """Test the finding of 'test' with the creation of modules listed in          'create'. @@ -182,7 +179,23 @@ class FinderTests(abc.FinderTests):              finder = self.get_finder(file_obj.name)              self.assertEqual((None, []), finder.find_loader('doesnotexist')) -Frozen_FinderTests, Source_FinderTests = util.test_both(FinderTests, machinery=machinery) +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 + +Frozen_FinderTestsPEP451, Source_FinderTestsPEP451 = util.test_both( +        FinderTestsPEP451, machinery=machinery) + + +class FinderTestsPEP302(FinderTests): + +    def import_(self, root, module): +        return self.get_finder(root).find_module(module) + +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 654f4c2b2f..aaf0041101 100644 --- a/Lib/test/test_importlib/source/test_source_encoding.py +++ b/Lib/test/test_importlib/source/test_source_encoding.py @@ -4,8 +4,10 @@ from . import util as source_util  machinery = util.import_importlib('importlib.machinery')  import codecs +import importlib.util  import re  import sys +import types  # Because sys.path gets essentially blanked, need to have unicodedata already  # imported for the parser to use.  import unicodedata @@ -39,7 +41,7 @@ class EncodingTest:                  file.write(source)              loader = self.machinery.SourceFileLoader(self.module_name,                                                    mapping[self.module_name]) -            return loader.load_module(self.module_name) +            return self.load(loader)      def create_source(self, encoding):          encoding_line = "# coding={0}".format(encoding) @@ -86,7 +88,24 @@ class EncodingTest:          with self.assertRaises(SyntaxError):              self.run_test(source) -Frozen_EncodingTest, Source_EncodingTest = util.test_both(EncodingTest, machinery=machinery) +class EncodingTestPEP451(EncodingTest): + +    def load(self, loader): +        module = types.ModuleType(self.module_name) +        module.__spec__ = importlib.util.spec_from_loader(self.module_name, loader) +        loader.exec_module(module) +        return module + +Frozen_EncodingTestPEP451, Source_EncodingTestPEP451 = util.test_both( +        EncodingTestPEP451, machinery=machinery) + +class EncodingTestPEP302(EncodingTest): + +    def load(self, loader): +        return loader.load_module(self.module_name) + +Frozen_EncodingTestPEP302, Source_EncodingTestPEP302 = util.test_both( +        EncodingTestPEP302, machinery=machinery)  class LineEndingTest: @@ -117,8 +136,24 @@ class LineEndingTest:      def test_lf(self):          self.run_test(b'\n') -Frozen_LineEndings, Source_LineEndings = util.test_both(LineEndingTest, machinery=machinery) +class LineEndingTestPEP451(LineEndingTest): + +    def load(self, loader): +        module = types.ModuleType(self.module_name) +        module.__spec__ = importlib.util.spec_from_loader(self.module_name, loader) +        loader.exec_module(module) +        return module + +Frozen_LineEndingTestPEP451, Source_LineEndingTestPEP451 = util.test_both( +        LineEndingTestPEP451, machinery=machinery) + +class LineEndingTestPEP302(LineEndingTest): + +    def load(self, loader): +        return loader.load_module(self.module_name) +Frozen_LineEndingTestPEP302, Source_LineEndingTestPEP302 = util.test_both( +        LineEndingTestPEP302, machinery=machinery)  if __name__ == '__main__':  | 
