diff options
Diffstat (limited to 'Lib/imp.py')
| -rw-r--r-- | Lib/imp.py | 194 | 
1 files changed, 105 insertions, 89 deletions
diff --git a/Lib/imp.py b/Lib/imp.py index 40869f5ac0..458d3702fe 100644 --- a/Lib/imp.py +++ b/Lib/imp.py @@ -12,22 +12,24 @@ from _imp import (lock_held, acquire_lock, release_lock,                    _fix_co_filename)  try:      from _imp import load_dynamic -except ImportError: +except ModuleNotFoundError:      # Platform doesn't support dynamic loading.      load_dynamic = None -# Directly exposed by this module -from importlib._bootstrap import new_module -from importlib._bootstrap import cache_from_source, source_from_cache +from importlib._bootstrap import SourcelessFileLoader, _ERR_MSG - -from importlib import _bootstrap  from importlib import machinery +from importlib import util +import importlib  import os  import sys  import tokenize +import types  import warnings +warnings.warn("the imp module is deprecated in favour of importlib; " +              "see the module's documentation for alternative uses", +              PendingDeprecationWarning)  # DEPRECATED  SEARCH_ERROR = 0 @@ -42,9 +44,23 @@ PY_CODERESOURCE = 8  IMP_HOOK = 9 +def new_module(name): +    """**DEPRECATED** + +    Create a new module. + +    The module is not entered into sys.modules. + +    """ +    return types.ModuleType(name) + +  def get_magic(): -    """Return the magic number for .pyc or .pyo files.""" -    return _bootstrap._MAGIC_BYTES +    """**DEPRECATED** + +    Return the magic number for .pyc or .pyo files. +    """ +    return util.MAGIC_NUMBER  def get_tag(): @@ -52,10 +68,40 @@ def get_tag():      return sys.implementation.cache_tag +def cache_from_source(path, debug_override=None): +    """**DEPRECATED** + +    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 +    .pyc/.pyo file calculated as if the .py file were imported.  The extension +    will be .pyc unless sys.flags.optimize is non-zero, then it will be .pyo. + +    If debug_override is not None, then it must be a boolean and is used in +    place of sys.flags.optimize. + +    If sys.implementation.cache_tag is None then NotImplementedError is raised. + +    """ +    return util.cache_from_source(path, debug_override) + + +def source_from_cache(path): +    """**DEPRECATED** + +    Given the path to a .pyc./.pyo file, return the path to its .py file. + +    The .pyc/.pyo file does not need to exist; this simply returns the path to +    the .py file calculated to correspond to the .pyc/.pyo file.  If path does +    not conform to PEP 3147 format, ValueError will be raised. If +    sys.implementation.cache_tag is None then NotImplementedError is raised. + +    """ +    return util.source_from_cache(path) + +  def get_suffixes(): -    warnings.warn('imp.get_suffixes() is deprecated; use the constants ' -                  'defined on importlib.machinery instead', -                  DeprecationWarning, 2) +    """**DEPRECATED**"""      extensions = [(s, 'rb', C_EXTENSION) for s in machinery.EXTENSION_SUFFIXES]      source = [(s, 'U', PY_SOURCE) for s in machinery.SOURCE_SUFFIXES]      bytecode = [(s, 'rb', PY_COMPILED) for s in machinery.BYTECODE_SUFFIXES] @@ -65,7 +111,11 @@ def get_suffixes():  class NullImporter: -    """Null import object.""" +    """**DEPRECATED** + +    Null import object. + +    """      def __init__(self, path):          if path == '': @@ -101,48 +151,37 @@ class _HackedGetData:              return super().get_data(path) -class _LoadSourceCompatibility(_HackedGetData, _bootstrap.SourceFileLoader): +class _LoadSourceCompatibility(_HackedGetData, machinery.SourceFileLoader):      """Compatibility support for implementing load_source()."""  def load_source(name, pathname, file=None): -    msg = ('imp.load_source() is deprecated; use ' -           'importlib.machinery.SourceFileLoader(name, pathname).load_module()' -           ' instead') -    warnings.warn(msg, DeprecationWarning, 2)      _LoadSourceCompatibility(name, pathname, file).load_module(name)      module = sys.modules[name]      # To allow reloading to potentially work, use a non-hacked loader which      # won't rely on a now-closed file object. -    module.__loader__ = _bootstrap.SourceFileLoader(name, pathname) +    module.__loader__ = machinery.SourceFileLoader(name, pathname)      return module -class _LoadCompiledCompatibility(_HackedGetData, -        _bootstrap.SourcelessFileLoader): +class _LoadCompiledCompatibility(_HackedGetData, SourcelessFileLoader):      """Compatibility support for implementing load_compiled()."""  def load_compiled(name, pathname, file=None): -    msg = ('imp.load_compiled() is deprecated; use ' -           'importlib.machinery.SourcelessFileLoader(name, pathname).' -           'load_module() instead ') -    warnings.warn(msg, DeprecationWarning, 2) +    """**DEPRECATED**"""      _LoadCompiledCompatibility(name, pathname, file).load_module(name)      module = sys.modules[name]      # To allow reloading to potentially work, use a non-hacked loader which      # won't rely on a now-closed file object. -    module.__loader__ = _bootstrap.SourcelessFileLoader(name, pathname) +    module.__loader__ = SourcelessFileLoader(name, pathname)      return module  def load_package(name, path): -    msg = ('imp.load_package() is deprecated; use either ' -           'importlib.machinery.SourceFileLoader() or ' -           'importlib.machinery.SourcelessFileLoader() instead') -    warnings.warn(msg, DeprecationWarning, 2) +    """**DEPRECATED**"""      if os.path.isdir(path):          extensions = (machinery.SOURCE_SUFFIXES[:] +                        machinery.BYTECODE_SUFFIXES[:]) @@ -152,7 +191,7 @@ def load_package(name, path):                  break          else:              raise ValueError('{!r} is not a package'.format(path)) -    return _bootstrap.SourceFileLoader(name, path).load_module(name) +    return machinery.SourceFileLoader(name, path).load_module(name)  def load_module(name, file, filename, details): @@ -164,32 +203,30 @@ def load_module(name, file, filename, details):      """      suffix, mode, type_ = details -    with warnings.catch_warnings(): -        warnings.simplefilter('ignore') -        if mode and (not mode.startswith(('r', 'U')) or '+' in mode): -            raise ValueError('invalid file open mode {!r}'.format(mode)) -        elif file is None and type_ in {PY_SOURCE, PY_COMPILED}: -            msg = 'file object required for import (type code {})'.format(type_) -            raise ValueError(msg) -        elif type_ == PY_SOURCE: -            return load_source(name, filename, file) -        elif type_ == PY_COMPILED: -            return load_compiled(name, filename, file) -        elif type_ == C_EXTENSION and load_dynamic is not None: -            if file is None: -                with open(filename, 'rb') as opened_file: -                    return load_dynamic(name, filename, opened_file) -            else: -                return load_dynamic(name, filename, file) -        elif type_ == PKG_DIRECTORY: -            return load_package(name, filename) -        elif type_ == C_BUILTIN: -            return init_builtin(name) -        elif type_ == PY_FROZEN: -            return init_frozen(name) +    if mode and (not mode.startswith(('r', 'U')) or '+' in mode): +        raise ValueError('invalid file open mode {!r}'.format(mode)) +    elif file is None and type_ in {PY_SOURCE, PY_COMPILED}: +        msg = 'file object required for import (type code {})'.format(type_) +        raise ValueError(msg) +    elif type_ == PY_SOURCE: +        return load_source(name, filename, file) +    elif type_ == PY_COMPILED: +        return load_compiled(name, filename, file) +    elif type_ == C_EXTENSION and load_dynamic is not None: +        if file is None: +            with open(filename, 'rb') as opened_file: +                return load_dynamic(name, filename, opened_file)          else: -            msg =  "Don't know how to import {} (type code {})".format(name, type_) -            raise ImportError(msg, name=name) +            return load_dynamic(name, filename, file) +    elif type_ == PKG_DIRECTORY: +        return load_package(name, filename) +    elif type_ == C_BUILTIN: +        return init_builtin(name) +    elif type_ == PY_FROZEN: +        return init_frozen(name) +    else: +        msg =  "Don't know how to import {} (type code {})".format(name, type_) +        raise ImportError(msg, name=name)  def find_module(name, path=None): @@ -225,18 +262,16 @@ def find_module(name, path=None):              file_path = os.path.join(package_directory, package_file_name)              if os.path.isfile(file_path):                  return None, package_directory, ('', '', PKG_DIRECTORY) -        with warnings.catch_warnings(): -            warnings.simplefilter('ignore') -            for suffix, mode, type_ in get_suffixes(): -                file_name = name + suffix -                file_path = os.path.join(entry, file_name) -                if os.path.isfile(file_path): -                    break -            else: -                continue -            break  # Break out of outer loop when breaking out of inner loop. +        for suffix, mode, type_ in get_suffixes(): +            file_name = name + suffix +            file_path = os.path.join(entry, file_name) +            if os.path.isfile(file_path): +                break +        else: +            continue +        break  # Break out of outer loop when breaking out of inner loop.      else: -        raise ImportError(_bootstrap._ERR_MSG.format(name), name=name) +        raise ImportError(_ERR_MSG.format(name), name=name)      encoding = None      if mode == 'U': @@ -246,31 +281,12 @@ def find_module(name, path=None):      return file, file_path, (suffix, mode, type_) -_RELOADING = {} -  def reload(module): -    """Reload the module and return it. +    """**DEPRECATED** + +    Reload the module and return it.      The module must have been successfully imported before.      """ -    if not module or type(module) != type(sys): -        raise TypeError("reload() argument must be module") -    name = module.__name__ -    if name not in sys.modules: -        msg = "module {} not in sys.modules" -        raise ImportError(msg.format(name), name=name) -    if name in _RELOADING: -        return _RELOADING[name] -    _RELOADING[name] = module -    try: -        parent_name = name.rpartition('.')[0] -        if parent_name and parent_name not in sys.modules: -            msg = "parent {!r} not in sys.modules" -            raise ImportError(msg.format(parentname), name=parent_name) -        return module.__loader__.load_module(name) -    finally: -        try: -            del _RELOADING[name] -        except KeyError: -            pass +    return importlib.reload(module)  | 
