Commit 1410d87f authored by isidentical's avatar isidentical Committed by Batuhan Taskaya

Upgrade setuptools.depends to importlib from depracated imp

parent cb64d3a8
import sys import sys
import imp
import marshal import marshal
from distutils.version import StrictVersion from distutils.version import StrictVersion
from imp import PKG_DIRECTORY, PY_COMPILED, PY_SOURCE, PY_FROZEN from setuptools.extern import six
from .py33compat import Bytecode from .py33compat import Bytecode
if six.PY2:
import imp
from imp import PKG_DIRECTORY, PY_COMPILED, PY_SOURCE, PY_FROZEN
else:
import os.path
from importlib.util import find_spec, spec_from_loader
from importlib.machinery import SOURCE_SUFFIXES, BYTECODE_SUFFIXES, EXTENSION_SUFFIXES, BuiltinImporter, FrozenImporter
PY_SOURCE = 1
PY_COMPILED = 2
C_EXTENSION = 3
C_BUILTIN = 6
PY_FROZEN = 7
__all__ = [ __all__ = [
'Require', 'find_module', 'get_module_constant', 'extract_constant' 'Require', 'find_module', 'get_module_constant', 'extract_constant'
...@@ -81,21 +93,59 @@ class Require: ...@@ -81,21 +93,59 @@ class Require:
def find_module(module, paths=None): def find_module(module, paths=None):
"""Just like 'imp.find_module()', but with package support""" """Just like 'imp.find_module()', but with package support"""
if six.PY3:
spec = 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 = 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, FrozenImporter):
kind = PY_FROZEN
path = None # imp compabilty
suffix = mode = '' # imp compability
elif spec.origin == 'built-in' or static and issubclass(spec.loader, BuiltinImporter):
kind = C_BUILTIN
path = None # imp compabilty
suffix = mode = '' # imp compability
elif spec.has_location:
frozen = False
path = spec.origin
suffix = os.path.splitext(path)[1]
mode = 'r' if suffix in SOURCE_SUFFIXES else 'rb'
if suffix in SOURCE_SUFFIXES:
kind = PY_SOURCE
elif suffix in BYTECODE_SUFFIXES:
kind = PY_COMPILED
elif suffix in EXTENSION_SUFFIXES:
kind = C_EXTENSION
if kind in {PY_SOURCE, PY_COMPILED}:
file = open(path, mode)
else:
path = None
suffix = mode= ''
parts = module.split('.') return file, path, (suffix, mode, kind)
while parts: else:
part = parts.pop(0) parts = module.split('.')
f, path, (suffix, mode, kind) = info = imp.find_module(part, paths) while parts:
part = parts.pop(0)
f, path, (suffix, mode, kind) = info = imp.find_module(part, paths)
if kind == PKG_DIRECTORY: if kind == PKG_DIRECTORY:
parts = parts or ['__init__'] parts = parts or ['__init__']
paths = [path] paths = [path]
elif parts: elif parts:
raise ImportError("Can't find %r in %s" % (parts, module)) raise ImportError("Can't find %r in %s" % (parts, module))
return info return info
def get_module_constant(module, symbol, default=-1, paths=None): def get_module_constant(module, symbol, default=-1, paths=None):
...@@ -111,18 +161,29 @@ def get_module_constant(module, symbol, default=-1, paths=None): ...@@ -111,18 +161,29 @@ def get_module_constant(module, symbol, default=-1, paths=None):
# Module doesn't exist # Module doesn't exist
return None return None
if six.PY3:
spec = find_spec(module, paths)
if hasattr(spec, 'submodule_search_locations'):
spec = spec_from_loader('__init__.py', spec.loader)
try: try:
if kind == PY_COMPILED: if kind == PY_COMPILED:
f.read(8) # skip magic & date f.read(8) # skip magic & date
code = marshal.load(f) code = marshal.load(f)
elif kind == PY_FROZEN: elif kind == PY_FROZEN:
code = imp.get_frozen_object(module) if six.PY2:
code = imp.get_frozen_object(module)
else:
code = spec.loader.get_code(module)
elif kind == PY_SOURCE: elif kind == PY_SOURCE:
code = compile(f.read(), path, 'exec') code = compile(f.read(), path, 'exec')
else: else:
# Not something we can parse; we'll have to import it. :( # Not something we can parse; we'll have to import it. :(
if module not in sys.modules: if module not in sys.modules:
imp.load_module(module, f, path, (suffix, mode, kind)) if six.PY2:
imp.load_module(module, f, path, (suffix, mode, kind))
else:
sys.modules[module] = module_from_spec(spec)
return getattr(sys.modules[module], symbol, None) return getattr(sys.modules[module], symbol, None)
finally: finally:
......
...@@ -143,6 +143,7 @@ def test_build_deps_on_distutils(request, tmpdir_factory, build_dep): ...@@ -143,6 +143,7 @@ def test_build_deps_on_distutils(request, tmpdir_factory, build_dep):
'tests_require', 'tests_require',
'python_requires', 'python_requires',
'install_requires', 'install_requires',
'long_description_content_type',
] ]
assert not match or match.group(1).strip('"\'') in allowed_unknowns assert not match or match.group(1).strip('"\'') in allowed_unknowns
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment