summaryrefslogtreecommitdiffstats
path: root/Lib/importlib
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2012-05-04 19:20:40 (GMT)
committerBrett Cannon <brett@python.org>2012-05-04 19:20:40 (GMT)
commit2657df47449dd5d324985a5eb43b937217e0d7e0 (patch)
tree9010c343fd2387a7961009b5b2ed93c7176fc6b1 /Lib/importlib
parent17098a5447f8bc742023b39eb7d8ef141beed119 (diff)
downloadcpython-2657df47449dd5d324985a5eb43b937217e0d7e0.zip
cpython-2657df47449dd5d324985a5eb43b937217e0d7e0.tar.gz
cpython-2657df47449dd5d324985a5eb43b937217e0d7e0.tar.bz2
Issue #13959: Re-implement imp.get_suffixes() in Lib/imp.py.
This introduces a new function, imp.extension_suffixes(), which is currently undocumented. That is forthcoming once issue #14657 is resolved and how to expose file suffixes is decided.
Diffstat (limited to 'Lib/importlib')
-rw-r--r--Lib/importlib/_bootstrap.py45
-rw-r--r--Lib/importlib/test/extension/test_case_sensitivity.py2
-rw-r--r--Lib/importlib/test/extension/test_finder.py2
-rw-r--r--Lib/importlib/test/extension/test_path_hook.py2
-rw-r--r--Lib/importlib/test/source/test_case_sensitivity.py4
-rw-r--r--Lib/importlib/test/source/test_finder.py8
-rw-r--r--Lib/importlib/test/source/test_path_hook.py2
7 files changed, 14 insertions, 51 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
index b0c6a84..1b487ef 100644
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -95,16 +95,6 @@ def _path_split(path):
return front, tail
-def _path_exists(path):
- """Replacement for os.path.exists."""
- try:
- _os.stat(path)
- except OSError:
- return False
- else:
- return True
-
-
def _path_is_mode_type(path, mode):
"""Test whether the path is the specified mode type."""
try:
@@ -128,28 +118,6 @@ def _path_isdir(path):
return _path_is_mode_type(path, 0o040000)
-def _path_without_ext(path, ext_type):
- """Replacement for os.path.splitext()[0]."""
- for suffix in _suffix_list(ext_type):
- if path.endswith(suffix):
- return path[:-len(suffix)]
- else:
- raise ValueError("path is not of the specified type")
-
-
-def _path_absolute(path):
- """Replacement for os.path.abspath."""
- if not path:
- path = _os.getcwd()
- try:
- return _os._getfullpathname(path)
- except AttributeError:
- if path.startswith('/'):
- return path
- else:
- return _path_join(_os.getcwd(), path)
-
-
def _write_atomic(path, data):
"""Best-effort function to write data to a path atomically.
Be prepared to handle a FileExistsError if concurrent writing of the
@@ -338,12 +306,6 @@ def _requires_frozen(fxn):
return _requires_frozen_wrapper
-def _suffix_list(suffix_type):
- """Return a list of file suffixes based on the imp file type."""
- return [suffix[0] for suffix in _imp.get_suffixes()
- if suffix[2] == suffix_type]
-
-
# Loaders #####################################################################
class BuiltinImporter:
@@ -1196,8 +1158,9 @@ def _install(sys_module, _imp_module):
"""
_setup(sys_module, _imp_module)
- supported_loaders = [(ExtensionFileLoader, _suffix_list(3), False),
- (SourceFileLoader, _suffix_list(1), True),
- (SourcelessFileLoader, _suffix_list(2), True)]
+ extensions = ExtensionFileLoader, _imp_module.extension_suffixes(), False
+ source = SourceFileLoader, _SOURCE_SUFFIXES, True
+ bytecode = SourcelessFileLoader, [_BYTECODE_SUFFIX], True
+ supported_loaders = [extensions, source, bytecode]
sys.path_hooks.extend([FileFinder.path_hook(*supported_loaders)])
sys.meta_path.extend([BuiltinImporter, FrozenImporter, PathFinder])
diff --git a/Lib/importlib/test/extension/test_case_sensitivity.py b/Lib/importlib/test/extension/test_case_sensitivity.py
index 1ba2a33..bdc21e7 100644
--- a/Lib/importlib/test/extension/test_case_sensitivity.py
+++ b/Lib/importlib/test/extension/test_case_sensitivity.py
@@ -16,7 +16,7 @@ class ExtensionModuleCaseSensitivityTest(unittest.TestCase):
assert good_name != bad_name
finder = _bootstrap.FileFinder(ext_util.PATH,
(_bootstrap.ExtensionFileLoader,
- _bootstrap._suffix_list(imp.C_EXTENSION),
+ imp.extension_suffixes(),
False))
return finder.find_module(bad_name)
diff --git a/Lib/importlib/test/extension/test_finder.py b/Lib/importlib/test/extension/test_finder.py
index a28cd07..804d862 100644
--- a/Lib/importlib/test/extension/test_finder.py
+++ b/Lib/importlib/test/extension/test_finder.py
@@ -12,7 +12,7 @@ class FinderTests(abc.FinderTests):
def find_module(self, fullname):
importer = _bootstrap.FileFinder(util.PATH,
(_bootstrap.ExtensionFileLoader,
- _bootstrap._suffix_list(imp.C_EXTENSION),
+ imp.extension_suffixes(),
False))
return importer.find_module(fullname)
diff --git a/Lib/importlib/test/extension/test_path_hook.py b/Lib/importlib/test/extension/test_path_hook.py
index 673c300..129e6e2 100644
--- a/Lib/importlib/test/extension/test_path_hook.py
+++ b/Lib/importlib/test/extension/test_path_hook.py
@@ -15,7 +15,7 @@ class PathHookTests(unittest.TestCase):
def hook(self, entry):
return _bootstrap.FileFinder.path_hook((_bootstrap.ExtensionFileLoader,
- _bootstrap._suffix_list(imp.C_EXTENSION), False))(entry)
+ imp.extension_suffixes(), False))(entry)
def test_success(self):
# Path hook should handle a directory where a known extension module
diff --git a/Lib/importlib/test/source/test_case_sensitivity.py b/Lib/importlib/test/source/test_case_sensitivity.py
index f65f285..fade25f 100644
--- a/Lib/importlib/test/source/test_case_sensitivity.py
+++ b/Lib/importlib/test/source/test_case_sensitivity.py
@@ -22,10 +22,10 @@ class CaseSensitivityTest(unittest.TestCase):
def find(self, path):
finder = _bootstrap.FileFinder(path,
(_bootstrap.SourceFileLoader,
- _bootstrap._suffix_list(imp.PY_SOURCE),
+ _bootstrap._SOURCE_SUFFIXES,
True),
(_bootstrap.SourcelessFileLoader,
- _bootstrap._suffix_list(imp.PY_COMPILED),
+ [_bootstrap._BYTECODE_SUFFIX],
True))
return finder.find_module(self.name)
diff --git a/Lib/importlib/test/source/test_finder.py b/Lib/importlib/test/source/test_finder.py
index 32ebd73..45b804f 100644
--- a/Lib/importlib/test/source/test_finder.py
+++ b/Lib/importlib/test/source/test_finder.py
@@ -37,9 +37,9 @@ class FinderTests(abc.FinderTests):
def import_(self, root, module):
loader_details = [(_bootstrap.SourceFileLoader,
- _bootstrap._suffix_list(imp.PY_SOURCE), True),
+ _bootstrap._SOURCE_SUFFIXES, True),
(_bootstrap.SourcelessFileLoader,
- _bootstrap._suffix_list(imp.PY_COMPILED), True)]
+ [_bootstrap._BYTECODE_SUFFIX], True)]
finder = _bootstrap.FileFinder(root, *loader_details)
return finder.find_module(module)
@@ -139,7 +139,7 @@ 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.SourceFileLoader,
- _bootstrap._suffix_list(imp.PY_SOURCE), True))
+ _bootstrap._SOURCE_SUFFIXES, True))
with open('mod.py', 'w') as file:
file.write("# test file for importlib")
try:
@@ -151,7 +151,7 @@ class FinderTests(abc.FinderTests):
def test_invalidate_caches(self):
# invalidate_caches() should reset the mtime.
finder = _bootstrap.FileFinder('', (_bootstrap.SourceFileLoader,
- _bootstrap._suffix_list(imp.PY_SOURCE), True))
+ _bootstrap._SOURCE_SUFFIXES, True))
finder._path_mtime = 42
finder.invalidate_caches()
self.assertEqual(finder._path_mtime, -1)
diff --git a/Lib/importlib/test/source/test_path_hook.py b/Lib/importlib/test/source/test_path_hook.py
index 663a128..df69b4d 100644
--- a/Lib/importlib/test/source/test_path_hook.py
+++ b/Lib/importlib/test/source/test_path_hook.py
@@ -11,7 +11,7 @@ class PathHookTest(unittest.TestCase):
def path_hook(self):
return _bootstrap.FileFinder.path_hook((_bootstrap.SourceFileLoader,
- _bootstrap._suffix_list(imp.PY_SOURCE), True))
+ _bootstrap._SOURCE_SUFFIXES, True))
def test_success(self):
with source_util.create_modules('dummy') as mapping: