Properly mark names in importlib._bootstrap as private.

This commit is contained in:
Brett Cannon 2012-05-04 13:52:49 -04:00
parent fbd85a0fbf
commit 17098a5447
2 changed files with 37 additions and 37 deletions

View File

@ -15,8 +15,8 @@ from _imp import get_magic, get_tag
# Can (probably) move to importlib # Can (probably) move to importlib
from _imp import get_suffixes from _imp import get_suffixes
from importlib._bootstrap import _new_module as new_module from importlib._bootstrap import new_module
from importlib._bootstrap import _cache_from_source as cache_from_source from importlib._bootstrap import cache_from_source
from importlib import _bootstrap from importlib import _bootstrap
import os import os
@ -48,14 +48,14 @@ def source_from_cache(path):
""" """
head, pycache_filename = os.path.split(path) head, pycache_filename = os.path.split(path)
head, pycache = os.path.split(head) head, pycache = os.path.split(head)
if pycache != _bootstrap.PYCACHE: if pycache != _bootstrap._PYCACHE:
raise ValueError('{} not bottom-level directory in ' raise ValueError('{} not bottom-level directory in '
'{!r}'.format(_bootstrap.PYCACHE, path)) '{!r}'.format(_bootstrap._PYCACHE, path))
if pycache_filename.count('.') != 2: if pycache_filename.count('.') != 2:
raise ValueError('expected only 2 dots in ' raise ValueError('expected only 2 dots in '
'{!r}'.format(pycache_filename)) '{!r}'.format(pycache_filename))
base_filename = pycache_filename.partition('.')[0] base_filename = pycache_filename.partition('.')[0]
return os.path.join(head, base_filename + _bootstrap.SOURCE_SUFFIXES[0]) return os.path.join(head, base_filename + _bootstrap._SOURCE_SUFFIXES[0])
class NullImporter: class NullImporter:
@ -185,7 +185,7 @@ def find_module(name, path=None):
for entry in path: for entry in path:
package_directory = os.path.join(entry, name) package_directory = os.path.join(entry, name)
for suffix in ['.py', _bootstrap.BYTECODE_SUFFIX]: for suffix in ['.py', _bootstrap._BYTECODE_SUFFIX]:
package_file_name = '__init__' + suffix package_file_name = '__init__' + suffix
file_path = os.path.join(package_directory, package_file_name) file_path = os.path.join(package_directory, package_file_name)
if os.path.isfile(file_path): if os.path.isfile(file_path):

View File

@ -26,11 +26,11 @@ work. One should use importlib as the public-facing version of this module.
# Bootstrap-related code ###################################################### # Bootstrap-related code ######################################################
CASE_INSENSITIVE_PLATFORMS = 'win', 'cygwin', 'darwin' _CASE_INSENSITIVE_PLATFORMS = 'win', 'cygwin', 'darwin'
def _make_relax_case(): def _make_relax_case():
if sys.platform.startswith(CASE_INSENSITIVE_PLATFORMS): if sys.platform.startswith(_CASE_INSENSITIVE_PLATFORMS):
def _relax_case(): def _relax_case():
"""True if filenames must be checked case-insensitively.""" """True if filenames must be checked case-insensitively."""
return b'PYTHONCASEOK' in _os.environ return b'PYTHONCASEOK' in _os.environ
@ -179,10 +179,10 @@ def _wrap(new, old):
new.__dict__.update(old.__dict__) new.__dict__.update(old.__dict__)
code_type = type(_wrap.__code__) _code_type = type(_wrap.__code__)
def _new_module(name): def new_module(name):
"""Create a new module. """Create a new module.
The module is not entered into sys.modules. The module is not entered into sys.modules.
@ -193,15 +193,15 @@ def _new_module(name):
# Finder/loader utility code ################################################## # Finder/loader utility code ##################################################
PYCACHE = '__pycache__' _PYCACHE = '__pycache__'
SOURCE_SUFFIXES = ['.py'] # _setup() adds .pyw as needed. _SOURCE_SUFFIXES = ['.py'] # _setup() adds .pyw as needed.
DEBUG_BYTECODE_SUFFIX = '.pyc' _DEBUG_BYTECODE_SUFFIX = '.pyc'
OPT_BYTECODE_SUFFIX = '.pyo' _OPT_BYTECODE_SUFFIX = '.pyo'
BYTECODE_SUFFIX = DEBUG_BYTECODE_SUFFIX if __debug__ else OPT_BYTECODE_SUFFIX _BYTECODE_SUFFIX = _DEBUG_BYTECODE_SUFFIX if __debug__ else _OPT_BYTECODE_SUFFIX
def _cache_from_source(path, debug_override=None): def cache_from_source(path, debug_override=None):
"""Given the path to a .py file, return the path to its .pyc/.pyo file. """Given the path to a .py file, return the path to its .pyc/.pyo file.
The .py file does not need to exist; this simply returns the path to the The .py file does not need to exist; this simply returns the path to the
@ -213,14 +213,14 @@ def _cache_from_source(path, debug_override=None):
""" """
debug = __debug__ if debug_override is None else debug_override debug = __debug__ if debug_override is None else debug_override
suffix = DEBUG_BYTECODE_SUFFIX if debug else OPT_BYTECODE_SUFFIX suffix = _DEBUG_BYTECODE_SUFFIX if debug else _OPT_BYTECODE_SUFFIX
head, tail = _path_split(path) head, tail = _path_split(path)
base_filename, sep, _ = tail.partition('.') base_filename, sep, _ = tail.partition('.')
filename = ''.join([base_filename, sep, _TAG, suffix]) filename = ''.join([base_filename, sep, _TAG, suffix])
return _path_join(head, PYCACHE, filename) return _path_join(head, _PYCACHE, filename)
def verbose_message(message, *args): def _verbose_message(message, *args):
"""Print the message to stderr if -v/PYTHONVERBOSE is turned on.""" """Print the message to stderr if -v/PYTHONVERBOSE is turned on."""
if sys.flags.verbose: if sys.flags.verbose:
if not message.startswith(('#', 'import ')): if not message.startswith(('#', 'import ')):
@ -277,7 +277,7 @@ def module_for_loader(fxn):
# This must be done before open() is called as the 'io' module # This must be done before open() is called as the 'io' module
# implicitly imports 'locale' and would otherwise trigger an # implicitly imports 'locale' and would otherwise trigger an
# infinite loop. # infinite loop.
module = _new_module(fullname) module = new_module(fullname)
sys.modules[fullname] = module sys.modules[fullname] = module
module.__loader__ = self module.__loader__ = self
try: try:
@ -472,11 +472,11 @@ class _LoaderBasics:
raise ImportError(msg, name=fullname, path=bytecode_path) raise ImportError(msg, name=fullname, path=bytecode_path)
elif len(raw_timestamp) != 4: elif len(raw_timestamp) != 4:
message = 'bad timestamp in {}'.format(fullname) message = 'bad timestamp in {}'.format(fullname)
verbose_message(message) _verbose_message(message)
raise EOFError(message) raise EOFError(message)
elif len(raw_size) != 4: elif len(raw_size) != 4:
message = 'bad size in {}'.format(fullname) message = 'bad size in {}'.format(fullname)
verbose_message(message) _verbose_message(message)
raise EOFError(message) raise EOFError(message)
if source_stats is not None: if source_stats is not None:
try: try:
@ -486,7 +486,7 @@ class _LoaderBasics:
else: else:
if _r_long(raw_timestamp) != source_mtime: if _r_long(raw_timestamp) != source_mtime:
message = 'bytecode is stale for {}'.format(fullname) message = 'bytecode is stale for {}'.format(fullname)
verbose_message(message) _verbose_message(message)
raise ImportError(message, name=fullname, raise ImportError(message, name=fullname,
path=bytecode_path) path=bytecode_path)
try: try:
@ -510,7 +510,7 @@ class _LoaderBasics:
code_object = self.get_code(name) code_object = self.get_code(name)
module.__file__ = self.get_filename(name) module.__file__ = self.get_filename(name)
if not sourceless: if not sourceless:
module.__cached__ = _cache_from_source(module.__file__) module.__cached__ = cache_from_source(module.__file__)
else: else:
module.__cached__ = module.__file__ module.__cached__ = module.__file__
module.__package__ = name module.__package__ = name
@ -573,7 +573,7 @@ class SourceLoader(_LoaderBasics):
""" """
source_path = self.get_filename(fullname) source_path = self.get_filename(fullname)
bytecode_path = _cache_from_source(source_path) bytecode_path = cache_from_source(source_path)
source_mtime = None source_mtime = None
if bytecode_path is not None: if bytecode_path is not None:
try: try:
@ -594,12 +594,12 @@ class SourceLoader(_LoaderBasics):
except (ImportError, EOFError): except (ImportError, EOFError):
pass pass
else: else:
verbose_message('{} matches {}', bytecode_path, _verbose_message('{} matches {}', bytecode_path,
source_path) source_path)
found = marshal.loads(bytes_data) found = marshal.loads(bytes_data)
if isinstance(found, code_type): if isinstance(found, _code_type):
_imp._fix_co_filename(found, source_path) _imp._fix_co_filename(found, source_path)
verbose_message('code object from {}', _verbose_message('code object from {}',
bytecode_path) bytecode_path)
return found return found
else: else:
@ -609,7 +609,7 @@ class SourceLoader(_LoaderBasics):
source_bytes = self.get_data(source_path) source_bytes = self.get_data(source_path)
code_object = compile(source_bytes, source_path, 'exec', code_object = compile(source_bytes, source_path, 'exec',
dont_inherit=True) dont_inherit=True)
verbose_message('code object from {}', source_path) _verbose_message('code object from {}', source_path)
if (not sys.dont_write_bytecode and bytecode_path is not None and if (not sys.dont_write_bytecode and bytecode_path is not None and
source_mtime is not None): source_mtime is not None):
data = bytearray(_MAGIC_NUMBER) data = bytearray(_MAGIC_NUMBER)
@ -618,7 +618,7 @@ class SourceLoader(_LoaderBasics):
data.extend(marshal.dumps(code_object)) data.extend(marshal.dumps(code_object))
try: try:
self.set_data(bytecode_path, data) self.set_data(bytecode_path, data)
verbose_message('wrote {!r}', bytecode_path) _verbose_message('wrote {!r}', bytecode_path)
except NotImplementedError: except NotImplementedError:
pass pass
return code_object return code_object
@ -687,7 +687,7 @@ class SourceFileLoader(FileLoader, SourceLoader):
return return
try: try:
_write_atomic(path, data) _write_atomic(path, data)
verbose_message('created {!r}', path) _verbose_message('created {!r}', path)
except (PermissionError, FileExistsError): except (PermissionError, FileExistsError):
# Don't worry if you can't write bytecode or someone is writing # Don't worry if you can't write bytecode or someone is writing
# it at the same time. # it at the same time.
@ -706,8 +706,8 @@ class SourcelessFileLoader(FileLoader, _LoaderBasics):
data = self.get_data(path) data = self.get_data(path)
bytes_data = self._bytes_from_bytecode(fullname, data, path, None) bytes_data = self._bytes_from_bytecode(fullname, data, path, None)
found = marshal.loads(bytes_data) found = marshal.loads(bytes_data)
if isinstance(found, code_type): if isinstance(found, _code_type):
verbose_message('code object from {!r}', path) _verbose_message('code object from {!r}', path)
return found return found
else: else:
raise ImportError("Non-code object in {}".format(path), raise ImportError("Non-code object in {}".format(path),
@ -738,7 +738,7 @@ class ExtensionFileLoader:
is_reload = fullname in sys.modules is_reload = fullname in sys.modules
try: try:
module = _imp.load_dynamic(fullname, self.path) module = _imp.load_dynamic(fullname, self.path)
verbose_message('extension module loaded from {!r}', self.path) _verbose_message('extension module loaded from {!r}', self.path)
return module return module
except: except:
if not is_reload and fullname in sys.modules: if not is_reload and fullname in sys.modules:
@ -908,7 +908,7 @@ class FileFinder:
new_name = name new_name = name
lower_suffix_contents.add(new_name) lower_suffix_contents.add(new_name)
self._path_cache = lower_suffix_contents self._path_cache = lower_suffix_contents
if sys.platform.startswith(CASE_INSENSITIVE_PLATFORMS): if sys.platform.startswith(_CASE_INSENSITIVE_PLATFORMS):
self._relaxed_path_cache = set(fn.lower() for fn in contents) self._relaxed_path_cache = set(fn.lower() for fn in contents)
@classmethod @classmethod
@ -1013,7 +1013,7 @@ def _find_and_load(name, import_):
elif name not in sys.modules: elif name not in sys.modules:
# The parent import may have already imported this module. # The parent import may have already imported this module.
loader.load_module(name) loader.load_module(name)
verbose_message('import {!r} # {!r}', name, loader) _verbose_message('import {!r} # {!r}', name, loader)
# Backwards-compatibility; be nicer to skip the dict lookup. # Backwards-compatibility; be nicer to skip the dict lookup.
module = sys.modules[name] module = sys.modules[name]
if parent: if parent:
@ -1185,7 +1185,7 @@ def _setup(sys_module, _imp_module):
setattr(self_module, '_MAGIC_NUMBER', _imp_module.get_magic()) setattr(self_module, '_MAGIC_NUMBER', _imp_module.get_magic())
setattr(self_module, '_TAG', _imp.get_tag()) setattr(self_module, '_TAG', _imp.get_tag())
if builtin_os == 'nt': if builtin_os == 'nt':
SOURCE_SUFFIXES.append('.pyw') _SOURCE_SUFFIXES.append('.pyw')
def _install(sys_module, _imp_module): def _install(sys_module, _imp_module):