def _path_without_ext(path, ext_type):
"""Replacement for os.path.splitext()[0]."""
- for suffix in suffix_list(ext_type):
+ for suffix in _suffix_list(ext_type):
if path.endswith(suffix):
return path[:-len(suffix)]
else:
return _path_join(_os.getcwd(), path)
-class closing:
+class _closing:
"""Simple replacement for contextlib.closing."""
self.obj.close()
-def wrap(new, old):
+def _wrap(new, old):
"""Simple substitute for functools.wraps."""
for replace in ['__module__', '__name__', '__doc__']:
setattr(new, replace, getattr(old, replace))
if not hasattr(module, '__path__'):
module.__package__ = module.__package__.rpartition('.')[0]
return module
- wrap(wrapper, fxn)
+ _wrap(wrapper, fxn)
return wrapper
if not hasattr(module, '__loader__'):
module.__loader__ = self
return module
- wrap(wrapper, fxn)
+ _wrap(wrapper, fxn)
return wrapper
raise
-def chained_path_hook(*path_hooks):
+def _chained_path_hook(*path_hooks):
"""Create a closure which sequentially checks path hooks to see which ones
(if any) can work with a path."""
def path_hook(entry):
if not finders:
raise ImportError("no finder found")
else:
- return ChainedFinder(*finders)
+ return _ChainedFinder(*finders)
return path_hook
-class ChainedFinder:
+class _ChainedFinder:
"""Finder that sequentially calls other finders."""
return None
-def check_name(method):
+def _check_name(method):
"""Decorator to verify that the module being requested matches the one the
loader can handle.
if self._name != name:
raise ImportError("loader cannot handle %s" % name)
return method(self, name, *args, **kwargs)
- wrap(inner, method)
+ _wrap(inner, method)
return inner
if is_pkg:
raise ValueError("extension modules cannot be packages")
- @check_name
+ @_check_name
@set_package
@set_loader
def load_module(self, fullname):
del sys.modules[fullname]
raise
- @check_name
+ @_check_name
def is_package(self, fullname):
"""Return False as an extension module can never be a package."""
return False
- @check_name
+ @_check_name
def get_code(self, fullname):
"""Return None as an extension module cannot create a code object."""
return None
- @check_name
+ @_check_name
def get_source(self, fullname):
"""Return None as extension modules have no source code."""
return None
-def suffix_list(suffix_type):
+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]
if not is_reload:
del sys.modules[fullname]
raise
- wrap(decorated, fxn)
+ _wrap(decorated, fxn)
return decorated
def _find_path(self, ext_type):
"""Find a path from the base path and the specified extension type that
exists, returning None if one is not found."""
- for suffix in suffix_list(ext_type):
+ for suffix in _suffix_list(ext_type):
path = self._base_path + suffix
if _path_exists(path):
return path
else:
return None
- @check_name
+ @_check_name
def source_path(self, fullname):
"""Return the path to an existing source file for the module, or None
if one cannot be found."""
# Not a property so that it is easy to override.
return self._find_path(imp.PY_SOURCE)
- @check_name
+ @_check_name
def get_source(self, fullname):
"""Return the source for the module as a string.
if source_path is None:
return None
import tokenize
- with closing(_io.FileIO(source_path, 'r')) as file: # Assuming bytes.
+ with _closing(_io.FileIO(source_path, 'r')) as file: # Assuming bytes.
encoding, lines = tokenize.detect_encoding(file.readline)
# XXX Will fail when passed to compile() if the encoding is
# anything other than UTF-8.
"""Return the data from path as raw bytes."""
return _io.FileIO(path, 'r').read() # Assuming bytes.
- @check_name
+ @_check_name
def is_package(self, fullname):
"""Return a boolean based on whether the module is a package.
"""Load a module from a source or bytecode file."""
- @check_name
+ @_check_name
def source_mtime(self, name):
"""Return the modification time of the source for the specified
module."""
return None
return int(_os.stat(source_path).st_mtime)
- @check_name
+ @_check_name
def bytecode_path(self, fullname):
"""Return the path to a bytecode file, or None if one does not
exist."""
# Not a property for easy overriding.
return self._find_path(imp.PY_COMPILED)
- @check_name
+ @_check_name
def write_bytecode(self, name, data):
"""Write out 'data' for the specified module, returning a boolean
signifying if the write-out actually occurred.
"""
bytecode_path = self.bytecode_path(name)
if not bytecode_path:
- bytecode_path = self._base_path + suffix_list(imp.PY_COMPILED)[0]
+ bytecode_path = self._base_path + _suffix_list(imp.PY_COMPILED)[0]
file = _io.FileIO(bytecode_path, 'w') # Assuming bytes.
try:
- with closing(file) as bytecode_file:
+ with _closing(file) as bytecode_file:
bytecode_file.write(data)
return True
except IOError as exc:
def __init__(self, path_entry):
# Assigning to _suffixes here instead of at the class level because
# imp is not imported at the time of class creation.
- self._suffixes = suffix_list(imp.C_EXTENSION)
+ self._suffixes = _suffix_list(imp.C_EXTENSION)
super().__init__(path_entry)
# Lack of imp during class creation means _suffixes is set here.
# Make sure that Python source files are listed first! Needed for an
# optimization by the loader.
- self._suffixes = suffix_list(imp.PY_SOURCE)
+ self._suffixes = _suffix_list(imp.PY_SOURCE)
super().__init__(path_entry)
def __init__(self, path_entry):
super().__init__(path_entry)
- self._suffixes += suffix_list(imp.PY_COMPILED)
+ self._suffixes += _suffix_list(imp.PY_COMPILED)
class PathFinder:
return None
-_DEFAULT_PATH_HOOK = chained_path_hook(ExtensionFileFinder, PyPycFileFinder)
+_DEFAULT_PATH_HOOK = _chained_path_hook(ExtensionFileFinder, PyPycFileFinder)
class _DefaultPathFinder(PathFinder):
return super()._path_importer_cache(path, _DEFAULT_PATH_HOOK)
-class ImportLockContext:
+class _ImportLockContext:
"""Context manager for the import lock."""
name = "{0}.{1}".format(package[:dot], name)
else:
name = package[:dot]
- with ImportLockContext():
+ with _ImportLockContext():
try:
return sys.modules[name]
except KeyError: