cpython: 07ef52e751f3 (original) (raw)
Mercurial > cpython
changeset 87795:07ef52e751f3
Issue #19712: Update test.test_importlib.source for PEP 451 [#19712]
Brett Cannon brett@python.org | |
---|---|
date | Fri, 06 Dec 2013 14:25:01 -0500 |
parents | 543c76769c14 |
children | 07cfe9f5561b |
files | Lib/test/test_importlib/source/test_case_sensitivity.py Lib/test/test_importlib/source/test_file_loader.py Lib/test/test_importlib/source/test_finder.py Lib/test/test_importlib/source/test_source_encoding.py |
diffstat | 4 files changed, 140 insertions(+), 24 deletions(-)[+] [-] Lib/test/test_importlib/source/test_case_sensitivity.py 31 Lib/test/test_importlib/source/test_file_loader.py 71 Lib/test/test_importlib/source/test_finder.py 21 Lib/test/test_importlib/source/test_source_encoding.py 41 |
line wrap: on
line diff
--- 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 finder(self, path):
return self.machinery.FileFinder(path,[](#l1.10) (self.machinery.SourceFileLoader,[](#l1.11) self.machinery.SOURCE_SUFFIXES),[](#l1.12) (self.machinery.SourcelessFileLoader,[](#l1.13) self.machinery.BYTECODE_SUFFIXES))[](#l1.14)
return finder.find_module(self.name)[](#l1.15)
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)[](#l1.23)
sensitive_finder = self.finder(sensitive_path)[](#l1.24)
insensitive_finder = self.finder(insensitive_path)[](#l1.25)
return self.find(sensitive_finder), self.find(insensitive_finder)[](#l1.26)
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'))[](#l1.34)
self.assertIsNotNone(sensitive)[](#l1.35) self.assertIn(self.name, sensitive.get_filename(self.name))[](#l1.36) self.assertIsNone(insensitive)[](#l1.37)
@@ -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'))[](#l1.43)
self.assertIsNotNone(sensitive)[](#l1.44) self.assertIn(self.name, sensitive.get_filename(self.name))[](#l1.45)
self.assertTrue(hasattr(insensitive, 'load_module'))[](#l1.46)
self.assertIsNotNone(insensitive)[](#l1.47) self.assertIn(self.name, insensitive.get_filename(self.name))[](#l1.48)
-Frozen_CaseSensitivityTest, Source_CaseSensitivityTest = util.test_both(
+class CaseSensitivityTestPEP302(CaseSensitivityTest):
+ +Frozen_CaseSensitivityTestPEP302, Source_CaseSensitivityTestPEP302 = util.test_both(
+ +class CaseSensitivityTestPEP451(CaseSensitivityTest):
- def find(self, finder):
found = finder.find_spec(self.name)[](#l1.61)
return found.loader if found is not None else found[](#l1.62)
+ +Frozen_CaseSensitivityTestPEP451, Source_CaseSensitivityTestPEP451 = util.test_both(
--- 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)[](#l2.7)
for attr in attributes:[](#l2.8)
self.assertEqual(getattr(orig_module, attr), value)[](#l2.9)
with self.assertRaises(SyntaxError):[](#l2.10) loader.load_module(name)[](#l2.11) for attr in attributes:[](#l2.12) self.assertEqual(getattr(orig_module, attr), value)[](#l2.13)
@@ -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')[](#l2.18)
# PEP 451[](#l2.19)
module = types.ModuleType('_temp')[](#l2.20)
module.__spec__ = self.util.spec_from_loader('_temp', loader)[](#l2.21)
loader.exec_module(module)[](#l2.22)
self.assertEqual(module.x, 5)[](#l2.23)
self.assertTrue(os.path.exists(compiled))[](#l2.24)
os.unlink(compiled)[](#l2.25)
# PEP 302[](#l2.26)
mod = loader.load_module('_temp') # XXX[](#l2.27) # Sanity checks.[](#l2.28) self.assertEqual(mod.__cached__, compiled)[](#l2.29) self.assertEqual(mod.x, 5)[](#l2.30) # The pyc file was created.[](#l2.31)
os.stat(compiled)[](#l2.32)
self.assertTrue(os.path.exists(compiled))[](#l2.33)
def test_unloadable(self): loader = self.machinery.SourceFileLoader('good name', {})
module = types.ModuleType('bad name')[](#l2.37)
module.__spec__ = self.machinery.ModuleSpec('bad name', loader)[](#l2.38)
with self.assertRaises(ImportError):[](#l2.39)
loader.exec_module(module)[](#l2.40) with self.assertRaises(ImportError):[](#l2.41) loader.load_module('bad name')[](#l2.42)
@@ -291,8 +307,23 @@ class BadBytecodeTest: lambda bc: b'\x00\x00\x00\x00' + bc[4:]) test('_temp', mapping, bc_path) +class BadBytecodeTestPEP451(BadBytecodeTest): -class SourceLoaderBadBytecodeTest(BadBytecodeTest):
- def import_(self, file, module_name):
loader = self.loader(module_name, file)[](#l2.52)
module = types.ModuleType(module_name)[](#l2.53)
module.__spec__ = self.util.spec_from_loader(module_name, loader)[](#l2.54)
loader.exec_module(module)[](#l2.55)
+ +class BadBytecodeTestPEP302(BadBytecodeTest): +
- def import_(self, file, module_name):
loader = self.loader(module_name, file)[](#l2.60)
module = loader.load_module(module_name)[](#l2.61)
self.assertIn(module_name, sys.modules)[](#l2.62)
+ + +class SourceLoaderBadBytecodeTest: @classmethod def setUpClass(cls): @@ -418,12 +449,24 @@ class SourceLoaderBadBytecodeTest(BadByt # 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,[](#l2.74)
+class SourceLoaderBadBytecodeTestPEP451(
+ +Frozen_SourceBadBytecodePEP451, Source_SourceBadBytecodePEP451 = util.test_both(
SourceLoaderBadBytecodeTestPEP451, importlib=importlib, machinery=machinery,[](#l2.80)
abc=importlib_abc, util=importlib_util)[](#l2.81)
+ +class SourceLoaderBadBytecodeTestPEP302(
+ +Frozen_SourceBadBytecodePEP302, Source_SourceBadBytecodePEP302 = util.test_both(
SourceLoaderBadBytecodeTestPEP302, importlib=importlib, machinery=machinery,[](#l2.88) abc=importlib_abc, util=importlib_util)[](#l2.89)
-class SourcelessLoaderBadBytecodeTest(BadBytecodeTest): +class SourcelessLoaderBadBytecodeTest: @classmethod def setUpClass(cls): @@ -482,8 +525,20 @@ class SourcelessLoaderBadBytecodeTest(Ba def test_non_code_marshal(self): self._test_non_code_marshal(del_source=True) -Frozen_SourcelessBadBytecode, Source_SourcelessBadBytecode = util.test_both(
SourcelessLoaderBadBytecodeTest, importlib=importlib,[](#l2.102)
+class SourcelessLoaderBadBytecodeTestPEP451(SourcelessLoaderBadBytecodeTest,
+ +Frozen_SourcelessBadBytecodePEP451, Source_SourcelessBadBytecodePEP451 = util.test_both(
SourcelessLoaderBadBytecodeTestPEP451, importlib=importlib,[](#l2.108)
machinery=machinery, abc=importlib_abc, util=importlib_util)[](#l2.109)
+ +class SourcelessLoaderBadBytecodeTestPEP302(SourcelessLoaderBadBytecodeTest,
+ +Frozen_SourcelessBadBytecodePEP302, Source_SourcelessBadBytecodePEP302 = util.test_both(
SourcelessLoaderBadBytecodeTestPEP302, importlib=importlib,[](#l2.116) machinery=machinery, abc=importlib_abc, util=importlib_util)[](#l2.117)
--- 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 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)[](#l3.21)
return found.loader if found is not None else found[](#l3.22)
+ +Frozen_FinderTestsPEP451, Source_FinderTestsPEP451 = util.test_both(
FinderTestsPEP451, machinery=machinery)[](#l3.25)
+ + +class FinderTestsPEP302(FinderTests): +
+ +Frozen_FinderTestsPEP302, Source_FinderTestsPEP302 = util.test_both(
FinderTestsPEP302, machinery=machinery)[](#l3.34)
--- 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)[](#l4.18)
return self.load(loader)[](#l4.19)
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)[](#l4.31)
module.__spec__ = importlib.util.spec_from_loader(self.module_name, loader)[](#l4.32)
loader.exec_module(module)[](#l4.33)
return module[](#l4.34)
+ +Frozen_EncodingTestPEP451, Source_EncodingTestPEP451 = util.test_both(
EncodingTestPEP451, machinery=machinery)[](#l4.37)
+ +class EncodingTestPEP302(EncodingTest): +
+ +Frozen_EncodingTestPEP302, Source_EncodingTestPEP302 = util.test_both(
EncodingTestPEP302, machinery=machinery)[](#l4.45)
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)[](#l4.57)
module.__spec__ = importlib.util.spec_from_loader(self.module_name, loader)[](#l4.58)
loader.exec_module(module)[](#l4.59)
return module[](#l4.60)
+Frozen_LineEndingTestPEP451, Source_LineEndingTestPEP451 = util.test_both(
LineEndingTestPEP451, machinery=machinery)[](#l4.63)
+ +class LineEndingTestPEP302(LineEndingTest): +
+ +Frozen_LineEndingTestPEP302, Source_LineEndingTestPEP302 = util.test_both(
LineEndingTestPEP302, machinery=machinery)[](#l4.71)