From 9e78161ca0853d19d8376ad0203563fbbf83e6ab Mon Sep 17 00:00:00 2001 From: DongHun Kwak Date: Wed, 30 Dec 2020 07:04:26 +0900 Subject: [PATCH] Imported Upstream version 41.6.0 --- .bumpversion.cfg | 2 +- CHANGES.rst | 12 ++++++++ conftest.py | 1 + setup.cfg | 2 +- setuptools/_imp.py | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ setuptools/depends.py | 48 +++++++++++++------------------ setuptools/msvc.py | 1 + setuptools/py27compat.py | 32 +++++++++++++++++++++ setuptools/py34compat.py | 13 +++++++++ 9 files changed, 153 insertions(+), 31 deletions(-) create mode 100644 setuptools/_imp.py create mode 100644 setuptools/py34compat.py diff --git a/.bumpversion.cfg b/.bumpversion.cfg index aeefc8f..40db5b0 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 41.5.0 +current_version = 41.6.0 commit = True tag = True diff --git a/CHANGES.rst b/CHANGES.rst index 7e9fc0f..ba7b464 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,3 +1,15 @@ +v41.6.0 +------- + +* #479: Replace usage of deprecated ``imp`` module with local re-implementation in ``setuptools._imp``. + + +v41.5.1 +------- + +* #1891: Fix code for detecting Visual Studio's version on Windows under Python 2. + + v41.5.0 ------- diff --git a/conftest.py b/conftest.py index 0d7b274..1746bfb 100644 --- a/conftest.py +++ b/conftest.py @@ -19,6 +19,7 @@ collect_ignore = [ if sys.version_info < (3,): collect_ignore.append('setuptools/lib2to3_ex.py') + collect_ignore.append('setuptools/_imp.py') if sys.version_info < (3, 6): diff --git a/setup.cfg b/setup.cfg index 0e030cd..42a3d86 100644 --- a/setup.cfg +++ b/setup.cfg @@ -19,7 +19,7 @@ universal = 1 [metadata] name = setuptools -version = 41.5.0 +version = 41.6.0 description = Easily download, build, install, upgrade, and uninstall Python packages author = Python Packaging Authority author_email = distutils-sig@python.org diff --git a/setuptools/_imp.py b/setuptools/_imp.py new file mode 100644 index 0000000..a3cce9b --- /dev/null +++ b/setuptools/_imp.py @@ -0,0 +1,73 @@ +""" +Re-implementation of find_module and get_frozen_object +from the deprecated imp module. +""" + +import os +import importlib.util +import importlib.machinery + +from .py34compat import module_from_spec + + +PY_SOURCE = 1 +PY_COMPILED = 2 +C_EXTENSION = 3 +C_BUILTIN = 6 +PY_FROZEN = 7 + + +def find_module(module, paths=None): + """Just like 'imp.find_module()', but with package support""" + spec = importlib.util.find_spec(module, paths) + if spec is None: + raise ImportError("Can't find %s" % module) + if not spec.has_location and hasattr(spec, 'submodule_search_locations'): + spec = importlib.util.spec_from_loader('__init__.py', spec.loader) + + kind = -1 + file = None + static = isinstance(spec.loader, type) + if spec.origin == 'frozen' or static and issubclass( + spec.loader, importlib.machinery.FrozenImporter): + kind = PY_FROZEN + path = None # imp compabilty + suffix = mode = '' # imp compability + elif spec.origin == 'built-in' or static and issubclass( + spec.loader, importlib.machinery.BuiltinImporter): + kind = C_BUILTIN + path = None # imp compabilty + suffix = mode = '' # imp compability + elif spec.has_location: + path = spec.origin + suffix = os.path.splitext(path)[1] + mode = 'r' if suffix in importlib.machinery.SOURCE_SUFFIXES else 'rb' + + if suffix in importlib.machinery.SOURCE_SUFFIXES: + kind = PY_SOURCE + elif suffix in importlib.machinery.BYTECODE_SUFFIXES: + kind = PY_COMPILED + elif suffix in importlib.machinery.EXTENSION_SUFFIXES: + kind = C_EXTENSION + + if kind in {PY_SOURCE, PY_COMPILED}: + file = open(path, mode) + else: + path = None + suffix = mode = '' + + return file, path, (suffix, mode, kind) + + +def get_frozen_object(module, paths=None): + spec = importlib.util.find_spec(module, paths) + if not spec: + raise ImportError("Can't find %s" % module) + return spec.loader.get_code(module) + + +def get_module(module, paths, info): + spec = importlib.util.find_spec(module, paths) + if not spec: + raise ImportError("Can't find %s" % module) + return module_from_spec(spec) diff --git a/setuptools/depends.py b/setuptools/depends.py index 45e7052..a37675c 100644 --- a/setuptools/depends.py +++ b/setuptools/depends.py @@ -1,11 +1,13 @@ import sys -import imp import marshal +import contextlib from distutils.version import StrictVersion -from imp import PKG_DIRECTORY, PY_COMPILED, PY_SOURCE, PY_FROZEN from .py33compat import Bytecode +from .py27compat import find_module, PY_COMPILED, PY_FROZEN, PY_SOURCE +from . import py27compat + __all__ = [ 'Require', 'find_module', 'get_module_constant', 'extract_constant' @@ -15,7 +17,8 @@ __all__ = [ class Require: """A prerequisite to building or installing a distribution""" - def __init__(self, name, requested_version, module, homepage='', + def __init__( + self, name, requested_version, module, homepage='', attribute=None, format=None): if format is None and requested_version is not None: @@ -79,23 +82,15 @@ class Require: return self.version_ok(version) -def find_module(module, paths=None): - """Just like 'imp.find_module()', but with package support""" - - parts = module.split('.') - - while parts: - part = parts.pop(0) - f, path, (suffix, mode, kind) = info = imp.find_module(part, paths) - - if kind == PKG_DIRECTORY: - parts = parts or ['__init__'] - paths = [path] - - elif parts: - raise ImportError("Can't find %r in %s" % (parts, module)) +def maybe_close(f): + @contextlib.contextmanager + def empty(): + yield + return + if not f: + return empty() - return info + return contextlib.closing(f) def get_module_constant(module, symbol, default=-1, paths=None): @@ -106,28 +101,23 @@ def get_module_constant(module, symbol, default=-1, paths=None): constant. Otherwise, return 'default'.""" try: - f, path, (suffix, mode, kind) = find_module(module, paths) + f, path, (suffix, mode, kind) = info = find_module(module, paths) except ImportError: # Module doesn't exist return None - try: + with maybe_close(f): if kind == PY_COMPILED: f.read(8) # skip magic & date code = marshal.load(f) elif kind == PY_FROZEN: - code = imp.get_frozen_object(module) + code = py27compat.get_frozen_object(module, paths) elif kind == PY_SOURCE: code = compile(f.read(), path, 'exec') else: # Not something we can parse; we'll have to import it. :( - if module not in sys.modules: - imp.load_module(module, f, path, (suffix, mode, kind)) - return getattr(sys.modules[module], symbol, None) - - finally: - if f: - f.close() + imported = py27compat.get_module(module, paths, info) + return getattr(imported, symbol, None) return extract_constant(code, symbol, default) diff --git a/setuptools/msvc.py b/setuptools/msvc.py index ffa7053..2ffe1c8 100644 --- a/setuptools/msvc.py +++ b/setuptools/msvc.py @@ -20,6 +20,7 @@ This may also support compilers shipped with compatible Visual Studio versions. """ import json +from io import open from os import listdir, pathsep from os.path import join, isfile, isdir, dirname import sys diff --git a/setuptools/py27compat.py b/setuptools/py27compat.py index 2985011..1d57360 100644 --- a/setuptools/py27compat.py +++ b/setuptools/py27compat.py @@ -2,6 +2,7 @@ Compatibility Support for Python 2.7 and earlier """ +import sys import platform from setuptools.extern import six @@ -26,3 +27,34 @@ linux_py2_ascii = ( rmtree_safe = str if linux_py2_ascii else lambda x: x """Workaround for http://bugs.python.org/issue24672""" + + +try: + from ._imp import find_module, PY_COMPILED, PY_FROZEN, PY_SOURCE + from ._imp import get_frozen_object, get_module +except ImportError: + import imp + from imp import PY_COMPILED, PY_FROZEN, PY_SOURCE # noqa + + def find_module(module, paths=None): + """Just like 'imp.find_module()', but with package support""" + parts = module.split('.') + while parts: + part = parts.pop(0) + f, path, (suffix, mode, kind) = info = imp.find_module(part, paths) + + if kind == imp.PKG_DIRECTORY: + parts = parts or ['__init__'] + paths = [path] + + elif parts: + raise ImportError("Can't find %r in %s" % (parts, module)) + + return info + + def get_frozen_object(module, paths): + return imp.get_frozen_object(module) + + def get_module(module, paths, info): + imp.load_module(module, *info) + return sys.modules[module] diff --git a/setuptools/py34compat.py b/setuptools/py34compat.py new file mode 100644 index 0000000..3ad9172 --- /dev/null +++ b/setuptools/py34compat.py @@ -0,0 +1,13 @@ +import importlib + +try: + import importlib.util +except ImportError: + pass + + +try: + module_from_spec = importlib.util.module_from_spec +except AttributeError: + def module_from_spec(spec): + return spec.loader.load_module(spec.name) -- 2.7.4