Kaydet (Commit) 2f8c8356 authored tarafından Brett Cannon's avatar Brett Cannon

Issue #19712: Update test.test_importlib.source for PEP 451

üst 86aae6a7
...@@ -21,13 +21,12 @@ class CaseSensitivityTest: ...@@ -21,13 +21,12 @@ class CaseSensitivityTest:
name = 'MoDuLe' name = 'MoDuLe'
assert name != name.lower() assert name != name.lower()
def find(self, path): def finder(self, path):
finder = self.machinery.FileFinder(path, return self.machinery.FileFinder(path,
(self.machinery.SourceFileLoader, (self.machinery.SourceFileLoader,
self.machinery.SOURCE_SUFFIXES), self.machinery.SOURCE_SUFFIXES),
(self.machinery.SourcelessFileLoader, (self.machinery.SourcelessFileLoader,
self.machinery.BYTECODE_SUFFIXES)) self.machinery.BYTECODE_SUFFIXES))
return finder.find_module(self.name)
def sensitivity_test(self): def sensitivity_test(self):
"""Look for a module with matching and non-matching sensitivity.""" """Look for a module with matching and non-matching sensitivity."""
...@@ -37,7 +36,9 @@ class CaseSensitivityTest: ...@@ -37,7 +36,9 @@ class CaseSensitivityTest:
with context as mapping: with context as mapping:
sensitive_path = os.path.join(mapping['.root'], 'sensitive') sensitive_path = os.path.join(mapping['.root'], 'sensitive')
insensitive_path = os.path.join(mapping['.root'], 'insensitive') 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): def test_sensitive(self):
with test_support.EnvironmentVarGuard() as env: with test_support.EnvironmentVarGuard() as env:
...@@ -46,7 +47,7 @@ class CaseSensitivityTest: ...@@ -46,7 +47,7 @@ class CaseSensitivityTest:
self.skipTest('os.environ changes not reflected in ' self.skipTest('os.environ changes not reflected in '
'_os.environ') '_os.environ')
sensitive, insensitive = self.sensitivity_test() sensitive, insensitive = self.sensitivity_test()
self.assertTrue(hasattr(sensitive, 'load_module')) self.assertIsNotNone(sensitive)
self.assertIn(self.name, sensitive.get_filename(self.name)) self.assertIn(self.name, sensitive.get_filename(self.name))
self.assertIsNone(insensitive) self.assertIsNone(insensitive)
...@@ -57,13 +58,25 @@ class CaseSensitivityTest: ...@@ -57,13 +58,25 @@ class CaseSensitivityTest:
self.skipTest('os.environ changes not reflected in ' self.skipTest('os.environ changes not reflected in '
'_os.environ') '_os.environ')
sensitive, insensitive = self.sensitivity_test() sensitive, insensitive = self.sensitivity_test()
self.assertTrue(hasattr(sensitive, 'load_module')) self.assertIsNotNone(sensitive)
self.assertIn(self.name, sensitive.get_filename(self.name)) 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)) self.assertIn(self.name, insensitive.get_filename(self.name))
Frozen_CaseSensitivityTest, Source_CaseSensitivityTest = util.test_both( class CaseSensitivityTestPEP302(CaseSensitivityTest):
CaseSensitivityTest, importlib=importlib, machinery=machinery) 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__': if __name__ == '__main__':
......
...@@ -120,6 +120,10 @@ class SimpleTest(abc.LoaderTests): ...@@ -120,6 +120,10 @@ class SimpleTest(abc.LoaderTests):
with open(mapping[name], 'w') as file: with open(mapping[name], 'w') as file:
file.write('+++ bad syntax +++') file.write('+++ bad syntax +++')
loader = self.machinery.SourceFileLoader('_temp', mapping['_temp']) 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): with self.assertRaises(SyntaxError):
loader.load_module(name) loader.load_module(name)
for attr in attributes: for attr in attributes:
...@@ -171,15 +175,27 @@ class SimpleTest(abc.LoaderTests): ...@@ -171,15 +175,27 @@ class SimpleTest(abc.LoaderTests):
raise raise
self.skipTest("cannot set modification time to large integer ({})".format(e)) self.skipTest("cannot set modification time to large integer ({})".format(e))
loader = self.machinery.SourceFileLoader('_temp', mapping['_temp']) 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. # Sanity checks.
self.assertEqual(mod.__cached__, compiled) self.assertEqual(mod.__cached__, compiled)
self.assertEqual(mod.x, 5) self.assertEqual(mod.x, 5)
# The pyc file was created. # The pyc file was created.
os.stat(compiled) self.assertTrue(os.path.exists(compiled))
def test_unloadable(self): def test_unloadable(self):
loader = self.machinery.SourceFileLoader('good name', {}) 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): with self.assertRaises(ImportError):
loader.load_module('bad name') loader.load_module('bad name')
...@@ -291,8 +307,23 @@ class BadBytecodeTest: ...@@ -291,8 +307,23 @@ class BadBytecodeTest:
lambda bc: b'\x00\x00\x00\x00' + bc[4:]) lambda bc: b'\x00\x00\x00\x00' + bc[4:])
test('_temp', mapping, bc_path) 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 @classmethod
def setUpClass(cls): def setUpClass(cls):
...@@ -418,12 +449,24 @@ class SourceLoaderBadBytecodeTest(BadBytecodeTest): ...@@ -418,12 +449,24 @@ class SourceLoaderBadBytecodeTest(BadBytecodeTest):
# Make writable for eventual clean-up. # Make writable for eventual clean-up.
os.chmod(bytecode_path, stat.S_IWUSR) os.chmod(bytecode_path, stat.S_IWUSR)
Frozen_SourceBadBytecode, Source_SourceBadBytecode = util.test_both( class SourceLoaderBadBytecodeTestPEP451(
SourceLoaderBadBytecodeTest, importlib=importlib, machinery=machinery, SourceLoaderBadBytecodeTest, BadBytecodeTestPEP451):
pass
Frozen_SourceBadBytecodePEP451, Source_SourceBadBytecodePEP451 = util.test_both(
SourceLoaderBadBytecodeTestPEP451, importlib=importlib, machinery=machinery,
abc=importlib_abc, util=importlib_util) 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 @classmethod
def setUpClass(cls): def setUpClass(cls):
...@@ -482,8 +525,20 @@ class SourcelessLoaderBadBytecodeTest(BadBytecodeTest): ...@@ -482,8 +525,20 @@ class SourcelessLoaderBadBytecodeTest(BadBytecodeTest):
def test_non_code_marshal(self): def test_non_code_marshal(self):
self._test_non_code_marshal(del_source=True) self._test_non_code_marshal(del_source=True)
Frozen_SourcelessBadBytecode, Source_SourcelessBadBytecode = util.test_both( class SourcelessLoaderBadBytecodeTestPEP451(SourcelessLoaderBadBytecodeTest,
SourcelessLoaderBadBytecodeTest, importlib=importlib, 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) machinery=machinery, abc=importlib_abc, util=importlib_util)
......
...@@ -46,9 +46,6 @@ class FinderTests(abc.FinderTests): ...@@ -46,9 +46,6 @@ class FinderTests(abc.FinderTests):
self.machinery.BYTECODE_SUFFIXES)] self.machinery.BYTECODE_SUFFIXES)]
return self.machinery.FileFinder(root, *loader_details) 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): def run_test(self, test, create=None, *, compile_=None, unlink=None):
"""Test the finding of 'test' with the creation of modules listed in """Test the finding of 'test' with the creation of modules listed in
'create'. 'create'.
...@@ -182,7 +179,23 @@ class FinderTests(abc.FinderTests): ...@@ -182,7 +179,23 @@ class FinderTests(abc.FinderTests):
finder = self.get_finder(file_obj.name) finder = self.get_finder(file_obj.name)
self.assertEqual((None, []), finder.find_loader('doesnotexist')) 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)
......
...@@ -4,8 +4,10 @@ from . import util as source_util ...@@ -4,8 +4,10 @@ from . import util as source_util
machinery = util.import_importlib('importlib.machinery') machinery = util.import_importlib('importlib.machinery')
import codecs import codecs
import importlib.util
import re import re
import sys import sys
import types
# Because sys.path gets essentially blanked, need to have unicodedata already # Because sys.path gets essentially blanked, need to have unicodedata already
# imported for the parser to use. # imported for the parser to use.
import unicodedata import unicodedata
...@@ -39,7 +41,7 @@ class EncodingTest: ...@@ -39,7 +41,7 @@ class EncodingTest:
file.write(source) file.write(source)
loader = self.machinery.SourceFileLoader(self.module_name, loader = self.machinery.SourceFileLoader(self.module_name,
mapping[self.module_name]) mapping[self.module_name])
return loader.load_module(self.module_name) return self.load(loader)
def create_source(self, encoding): def create_source(self, encoding):
encoding_line = "# coding={0}".format(encoding) encoding_line = "# coding={0}".format(encoding)
...@@ -86,7 +88,24 @@ class EncodingTest: ...@@ -86,7 +88,24 @@ class EncodingTest:
with self.assertRaises(SyntaxError): with self.assertRaises(SyntaxError):
self.run_test(source) 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: class LineEndingTest:
...@@ -117,8 +136,24 @@ class LineEndingTest: ...@@ -117,8 +136,24 @@ class LineEndingTest:
def test_lf(self): def test_lf(self):
self.run_test(b'\n') 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__': if __name__ == '__main__':
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment