Issue #14605: Expose importlib.abc.FileLoader and

importlib.machinery.(FileFinder, SourceFileLoader,
_SourcelessFileLoader, ExtensionFileLoader).

This exposes all of importlib's mechanisms that will become public on
the sys module.
This commit is contained in:
Brett Cannon 2012-04-22 19:58:33 -04:00
parent 8c5e920ae3
commit 938d44d59c
17 changed files with 3246 additions and 3076 deletions

View file

@ -2,6 +2,7 @@
from importlib import _bootstrap
from .. import util
from . import util as source_util
import imp
import os
import sys
from test import support as test_support
@ -19,9 +20,13 @@ class CaseSensitivityTest(unittest.TestCase):
assert name != name.lower()
def find(self, path):
finder = _bootstrap._FileFinder(path,
_bootstrap._SourceFinderDetails(),
_bootstrap._SourcelessFinderDetails())
finder = _bootstrap.FileFinder(path,
(_bootstrap.SourceFileLoader,
_bootstrap._suffix_list(imp.PY_SOURCE),
True),
(_bootstrap._SourcelessFileLoader,
_bootstrap._suffix_list(imp.PY_COMPILED),
True))
return finder.find_module(self.name)
def sensitivity_test(self):

View file

@ -27,7 +27,7 @@ class SimpleTest(unittest.TestCase):
# [basic]
def test_module(self):
with source_util.create_modules('_temp') as mapping:
loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp'])
loader = _bootstrap.SourceFileLoader('_temp', mapping['_temp'])
module = loader.load_module('_temp')
self.assertTrue('_temp' in sys.modules)
check = {'__name__': '_temp', '__file__': mapping['_temp'],
@ -37,7 +37,7 @@ class SimpleTest(unittest.TestCase):
def test_package(self):
with source_util.create_modules('_pkg.__init__') as mapping:
loader = _bootstrap._SourceFileLoader('_pkg',
loader = _bootstrap.SourceFileLoader('_pkg',
mapping['_pkg.__init__'])
module = loader.load_module('_pkg')
self.assertTrue('_pkg' in sys.modules)
@ -50,7 +50,7 @@ class SimpleTest(unittest.TestCase):
def test_lacking_parent(self):
with source_util.create_modules('_pkg.__init__', '_pkg.mod')as mapping:
loader = _bootstrap._SourceFileLoader('_pkg.mod',
loader = _bootstrap.SourceFileLoader('_pkg.mod',
mapping['_pkg.mod'])
module = loader.load_module('_pkg.mod')
self.assertTrue('_pkg.mod' in sys.modules)
@ -65,7 +65,7 @@ class SimpleTest(unittest.TestCase):
def test_module_reuse(self):
with source_util.create_modules('_temp') as mapping:
loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp'])
loader = _bootstrap.SourceFileLoader('_temp', mapping['_temp'])
module = loader.load_module('_temp')
module_id = id(module)
module_dict_id = id(module.__dict__)
@ -90,7 +90,7 @@ class SimpleTest(unittest.TestCase):
setattr(orig_module, attr, value)
with open(mapping[name], 'w') as file:
file.write('+++ bad syntax +++')
loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp'])
loader = _bootstrap.SourceFileLoader('_temp', mapping['_temp'])
with self.assertRaises(SyntaxError):
loader.load_module(name)
for attr in attributes:
@ -101,7 +101,7 @@ class SimpleTest(unittest.TestCase):
with source_util.create_modules('_temp') as mapping:
with open(mapping['_temp'], 'w') as file:
file.write('=')
loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp'])
loader = _bootstrap.SourceFileLoader('_temp', mapping['_temp'])
with self.assertRaises(SyntaxError):
loader.load_module('_temp')
self.assertTrue('_temp' not in sys.modules)
@ -114,7 +114,7 @@ class SimpleTest(unittest.TestCase):
file.write("# test file for importlib")
try:
with util.uncache('_temp'):
loader = _bootstrap._SourceFileLoader('_temp', file_path)
loader = _bootstrap.SourceFileLoader('_temp', file_path)
mod = loader.load_module('_temp')
self.assertEqual(file_path, mod.__file__)
self.assertEqual(imp.cache_from_source(file_path),
@ -140,7 +140,7 @@ class SimpleTest(unittest.TestCase):
if e.errno != getattr(errno, 'EOVERFLOW', None):
raise
self.skipTest("cannot set modification time to large integer ({})".format(e))
loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp'])
loader = _bootstrap.SourceFileLoader('_temp', mapping['_temp'])
mod = loader.load_module('_temp')
# Sanity checks.
self.assertEqual(mod.__cached__, compiled)
@ -255,7 +255,7 @@ class BadBytecodeTest(unittest.TestCase):
class SourceLoaderBadBytecodeTest(BadBytecodeTest):
loader = _bootstrap._SourceFileLoader
loader = _bootstrap.SourceFileLoader
@source_util.writes_bytecode_files
def test_empty_file(self):

View file

@ -3,6 +3,7 @@ from . import util as source_util
from importlib import _bootstrap
import errno
import imp
import os
import py_compile
from test.support import make_legacy_pyc
@ -35,9 +36,11 @@ class FinderTests(abc.FinderTests):
"""
def import_(self, root, module):
finder = _bootstrap._FileFinder(root,
_bootstrap._SourceFinderDetails(),
_bootstrap._SourcelessFinderDetails())
loader_details = [(_bootstrap.SourceFileLoader,
_bootstrap._suffix_list(imp.PY_SOURCE), True),
(_bootstrap._SourcelessFileLoader,
_bootstrap._suffix_list(imp.PY_COMPILED), True)]
finder = _bootstrap.FileFinder(root, *loader_details)
return finder.find_module(module)
def run_test(self, test, create=None, *, compile_=None, unlink=None):
@ -135,7 +138,8 @@ class FinderTests(abc.FinderTests):
def test_empty_string_for_dir(self):
# The empty string from sys.path means to search in the cwd.
finder = _bootstrap._FileFinder('', _bootstrap._SourceFinderDetails())
finder = _bootstrap.FileFinder('', (_bootstrap.SourceFileLoader,
_bootstrap._suffix_list(imp.PY_SOURCE), True))
with open('mod.py', 'w') as file:
file.write("# test file for importlib")
try:
@ -146,7 +150,8 @@ class FinderTests(abc.FinderTests):
def test_invalidate_caches(self):
# invalidate_caches() should reset the mtime.
finder = _bootstrap._FileFinder('', _bootstrap._SourceFinderDetails())
finder = _bootstrap.FileFinder('', (_bootstrap.SourceFileLoader,
_bootstrap._suffix_list(imp.PY_SOURCE), True))
finder._path_mtime = 42
finder.invalidate_caches()
self.assertEqual(finder._path_mtime, -1)

View file

@ -1,6 +1,7 @@
from . import util as source_util
from importlib import _bootstrap
import imp
import unittest
@ -8,14 +9,18 @@ class PathHookTest(unittest.TestCase):
"""Test the path hook for source."""
def path_hook(self):
return _bootstrap.FileFinder.path_hook((_bootstrap.SourceFileLoader,
_bootstrap._suffix_list(imp.PY_SOURCE), True))
def test_success(self):
with source_util.create_modules('dummy') as mapping:
self.assertTrue(hasattr(_bootstrap._file_path_hook(mapping['.root']),
self.assertTrue(hasattr(self.path_hook()(mapping['.root']),
'find_module'))
def test_empty_string(self):
# The empty string represents the cwd.
self.assertTrue(hasattr(_bootstrap._file_path_hook(''), 'find_module'))
self.assertTrue(hasattr(self.path_hook()(''), 'find_module'))
def test_main():

View file

@ -35,7 +35,7 @@ class EncodingTest(unittest.TestCase):
with source_util.create_modules(self.module_name) as mapping:
with open(mapping[self.module_name], 'wb') as file:
file.write(source)
loader = _bootstrap._SourceFileLoader(self.module_name,
loader = _bootstrap.SourceFileLoader(self.module_name,
mapping[self.module_name])
return loader.load_module(self.module_name)
@ -97,7 +97,7 @@ class LineEndingTest(unittest.TestCase):
with source_util.create_modules(module_name) as mapping:
with open(mapping[module_name], 'wb') as file:
file.write(source)
loader = _bootstrap._SourceFileLoader(module_name,
loader = _bootstrap.SourceFileLoader(module_name,
mapping[module_name])
return loader.load_module(module_name)