summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_importlib/source/test_file_loader.py
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2013-11-08 19:25:37 (GMT)
committerBrett Cannon <brett@python.org>2013-11-08 19:25:37 (GMT)
commit9ea8eda127b824f9cffb64deadbb420244a301c2 (patch)
tree4d5d779c6053a533f2e11b45d1378cdcc208dd2c /Lib/test/test_importlib/source/test_file_loader.py
parent3a4586a9f97c997fbdb0de297ed75374015e69bf (diff)
downloadcpython-9ea8eda127b824f9cffb64deadbb420244a301c2.zip
cpython-9ea8eda127b824f9cffb64deadbb420244a301c2.tar.gz
cpython-9ea8eda127b824f9cffb64deadbb420244a301c2.tar.bz2
Issue #16803: test.test_importlib.source now tests frozen and source code
Diffstat (limited to 'Lib/test/test_importlib/source/test_file_loader.py')
-rw-r--r--Lib/test/test_importlib/source/test_file_loader.py70
1 files changed, 43 insertions, 27 deletions
diff --git a/Lib/test/test_importlib/source/test_file_loader.py b/Lib/test/test_importlib/source/test_file_loader.py
index 9e035ea..f1e2713 100644
--- a/Lib/test/test_importlib/source/test_file_loader.py
+++ b/Lib/test/test_importlib/source/test_file_loader.py
@@ -1,11 +1,12 @@
-from importlib import machinery
-import importlib
-import importlib.abc
-import importlib.util
from .. import abc
from .. import util
from . import util as source_util
+importlib = util.import_importlib('importlib')
+importlib_abc = util.import_importlib('importlib.abc')
+machinery = util.import_importlib('importlib.machinery')
+importlib_util = util.import_importlib('importlib.util')
+
import errno
import marshal
import os
@@ -19,7 +20,7 @@ import unittest
from test.support import make_legacy_pyc, unload
-class SimpleTest(unittest.TestCase, abc.LoaderTests):
+class SimpleTest(abc.LoaderTests):
"""Should have no issue importing a source module [basic]. And if there is
a syntax error, it should raise a SyntaxError [syntax error].
@@ -27,7 +28,7 @@ class SimpleTest(unittest.TestCase, abc.LoaderTests):
"""
def test_load_module_API(self):
- class Tester(importlib.abc.FileLoader):
+ class Tester(self.abc.FileLoader):
def get_source(self, _): return 'attr = 42'
def is_package(self, _): return False
@@ -37,7 +38,7 @@ class SimpleTest(unittest.TestCase, abc.LoaderTests):
def test_get_filename_API(self):
# If fullname is not set then assume self.path is desired.
- class Tester(importlib.abc.FileLoader):
+ class Tester(self.abc.FileLoader):
def get_code(self, _): pass
def get_source(self, _): pass
def is_package(self, _): pass
@@ -55,7 +56,7 @@ class SimpleTest(unittest.TestCase, abc.LoaderTests):
# [basic]
def test_module(self):
with source_util.create_modules('_temp') as mapping:
- loader = machinery.SourceFileLoader('_temp', mapping['_temp'])
+ loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])
module = loader.load_module('_temp')
self.assertIn('_temp', sys.modules)
check = {'__name__': '_temp', '__file__': mapping['_temp'],
@@ -65,7 +66,7 @@ class SimpleTest(unittest.TestCase, abc.LoaderTests):
def test_package(self):
with source_util.create_modules('_pkg.__init__') as mapping:
- loader = machinery.SourceFileLoader('_pkg',
+ loader = self.machinery.SourceFileLoader('_pkg',
mapping['_pkg.__init__'])
module = loader.load_module('_pkg')
self.assertIn('_pkg', sys.modules)
@@ -78,7 +79,7 @@ class SimpleTest(unittest.TestCase, abc.LoaderTests):
def test_lacking_parent(self):
with source_util.create_modules('_pkg.__init__', '_pkg.mod')as mapping:
- loader = machinery.SourceFileLoader('_pkg.mod',
+ loader = self.machinery.SourceFileLoader('_pkg.mod',
mapping['_pkg.mod'])
module = loader.load_module('_pkg.mod')
self.assertIn('_pkg.mod', sys.modules)
@@ -93,7 +94,7 @@ class SimpleTest(unittest.TestCase, abc.LoaderTests):
def test_module_reuse(self):
with source_util.create_modules('_temp') as mapping:
- loader = machinery.SourceFileLoader('_temp', mapping['_temp'])
+ loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])
module = loader.load_module('_temp')
module_id = id(module)
module_dict_id = id(module.__dict__)
@@ -118,7 +119,7 @@ class SimpleTest(unittest.TestCase, abc.LoaderTests):
setattr(orig_module, attr, value)
with open(mapping[name], 'w') as file:
file.write('+++ bad syntax +++')
- loader = machinery.SourceFileLoader('_temp', mapping['_temp'])
+ loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])
with self.assertRaises(SyntaxError):
loader.load_module(name)
for attr in attributes:
@@ -129,7 +130,7 @@ class SimpleTest(unittest.TestCase, abc.LoaderTests):
with source_util.create_modules('_temp') as mapping:
with open(mapping['_temp'], 'w') as file:
file.write('=')
- loader = machinery.SourceFileLoader('_temp', mapping['_temp'])
+ loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])
with self.assertRaises(SyntaxError):
loader.load_module('_temp')
self.assertNotIn('_temp', sys.modules)
@@ -142,14 +143,14 @@ class SimpleTest(unittest.TestCase, abc.LoaderTests):
file.write("# test file for importlib")
try:
with util.uncache('_temp'):
- loader = machinery.SourceFileLoader('_temp', file_path)
+ loader = self.machinery.SourceFileLoader('_temp', file_path)
mod = loader.load_module('_temp')
self.assertEqual(file_path, mod.__file__)
- self.assertEqual(importlib.util.cache_from_source(file_path),
+ self.assertEqual(self.util.cache_from_source(file_path),
mod.__cached__)
finally:
os.unlink(file_path)
- pycache = os.path.dirname(importlib.util.cache_from_source(file_path))
+ pycache = os.path.dirname(self.util.cache_from_source(file_path))
if os.path.exists(pycache):
shutil.rmtree(pycache)
@@ -158,7 +159,7 @@ class SimpleTest(unittest.TestCase, abc.LoaderTests):
# truncated rather than raise an OverflowError.
with source_util.create_modules('_temp') as mapping:
source = mapping['_temp']
- compiled = importlib.util.cache_from_source(source)
+ compiled = self.util.cache_from_source(source)
with open(source, 'w') as f:
f.write("x = 5")
try:
@@ -169,7 +170,7 @@ class SimpleTest(unittest.TestCase, abc.LoaderTests):
if e.errno != getattr(errno, 'EOVERFLOW', None):
raise
self.skipTest("cannot set modification time to large integer ({})".format(e))
- loader = machinery.SourceFileLoader('_temp', mapping['_temp'])
+ loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])
mod = loader.load_module('_temp')
# Sanity checks.
self.assertEqual(mod.__cached__, compiled)
@@ -178,12 +179,16 @@ class SimpleTest(unittest.TestCase, abc.LoaderTests):
os.stat(compiled)
def test_unloadable(self):
- loader = machinery.SourceFileLoader('good name', {})
+ loader = self.machinery.SourceFileLoader('good name', {})
with self.assertRaises(ImportError):
loader.load_module('bad name')
+Frozen_SimpleTest, Source_SimpleTest = util.test_both(
+ SimpleTest, importlib=importlib, machinery=machinery, abc=importlib_abc,
+ util=importlib_util)
+
-class BadBytecodeTest(unittest.TestCase):
+class BadBytecodeTest:
def import_(self, file, module_name):
loader = self.loader(module_name, file)
@@ -200,7 +205,7 @@ class BadBytecodeTest(unittest.TestCase):
pass
py_compile.compile(mapping[name])
if not del_source:
- bytecode_path = importlib.util.cache_from_source(mapping[name])
+ bytecode_path = self.util.cache_from_source(mapping[name])
else:
os.unlink(mapping[name])
bytecode_path = make_legacy_pyc(mapping[name])
@@ -289,7 +294,9 @@ class BadBytecodeTest(unittest.TestCase):
class SourceLoaderBadBytecodeTest(BadBytecodeTest):
- loader = machinery.SourceFileLoader
+ @classmethod
+ def setUpClass(cls):
+ cls.loader = cls.machinery.SourceFileLoader
@source_util.writes_bytecode_files
def test_empty_file(self):
@@ -329,7 +336,7 @@ class SourceLoaderBadBytecodeTest(BadBytecodeTest):
self.import_(mapping[name], name)
with open(bytecode_path, 'rb') as bytecode_file:
self.assertEqual(bytecode_file.read(4),
- importlib.util.MAGIC_NUMBER)
+ self.util.MAGIC_NUMBER)
self._test_bad_magic(test)
@@ -379,13 +386,13 @@ class SourceLoaderBadBytecodeTest(BadBytecodeTest):
zeros = b'\x00\x00\x00\x00'
with source_util.create_modules('_temp') as mapping:
py_compile.compile(mapping['_temp'])
- bytecode_path = importlib.util.cache_from_source(mapping['_temp'])
+ bytecode_path = self.util.cache_from_source(mapping['_temp'])
with open(bytecode_path, 'r+b') as bytecode_file:
bytecode_file.seek(4)
bytecode_file.write(zeros)
self.import_(mapping['_temp'], '_temp')
source_mtime = os.path.getmtime(mapping['_temp'])
- source_timestamp = importlib._w_long(source_mtime)
+ source_timestamp = self.importlib._w_long(source_mtime)
with open(bytecode_path, 'rb') as bytecode_file:
bytecode_file.seek(4)
self.assertEqual(bytecode_file.read(4), source_timestamp)
@@ -397,7 +404,7 @@ class SourceLoaderBadBytecodeTest(BadBytecodeTest):
with source_util.create_modules('_temp') as mapping:
# Create bytecode that will need to be re-created.
py_compile.compile(mapping['_temp'])
- bytecode_path = importlib.util.cache_from_source(mapping['_temp'])
+ bytecode_path = self.util.cache_from_source(mapping['_temp'])
with open(bytecode_path, 'r+b') as bytecode_file:
bytecode_file.seek(0)
bytecode_file.write(b'\x00\x00\x00\x00')
@@ -411,10 +418,16 @@ 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,
+ abc=importlib_abc, util=importlib_util)
+
class SourcelessLoaderBadBytecodeTest(BadBytecodeTest):
- loader = machinery.SourcelessFileLoader
+ @classmethod
+ def setUpClass(cls):
+ cls.loader = cls.machinery.SourcelessFileLoader
def test_empty_file(self):
def test(name, mapping, bytecode_path):
@@ -469,6 +482,9 @@ 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,
+ machinery=machinery, abc=importlib_abc, util=importlib_util)
if __name__ == '__main__':