From dcc71f773576c19a3658735879893515b056ece5 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 26 Jul 2020 10:35:02 -0400 Subject: Rename _distutils_importer to _distutils_hack, as it supplies more than just an importer. --- _distutils_hack/__init__.py | 45 ++++++++++++++++++++++++++++++++++ _distutils_hack/install.py | 5 ++++ _distutils_hack/override.py | 54 +++++++++++++++++++++++++++++++++++++++++ _distutils_importer/__init__.py | 45 ---------------------------------- _distutils_importer/install.py | 5 ---- _distutils_importer/override.py | 54 ----------------------------------------- conftest.py | 2 +- setup.py | 2 +- setuptools/__init__.py | 2 +- setuptools/sandbox.py | 8 +++--- 10 files changed, 111 insertions(+), 111 deletions(-) create mode 100644 _distutils_hack/__init__.py create mode 100644 _distutils_hack/install.py create mode 100644 _distutils_hack/override.py delete mode 100644 _distutils_importer/__init__.py delete mode 100644 _distutils_importer/install.py delete mode 100644 _distutils_importer/override.py diff --git a/_distutils_hack/__init__.py b/_distutils_hack/__init__.py new file mode 100644 index 00000000..3ad70100 --- /dev/null +++ b/_distutils_hack/__init__.py @@ -0,0 +1,45 @@ +import sys +import os + + +def enabled(): + """ + Allow selection of distutils by environment variable. + """ + which = os.environ.get('SETUPTOOLS_USE_DISTUTILS', 'stdlib') + return which == 'local' + + +class DistutilsMetaFinder: + def find_spec(self, fullname, path, target=None): + if path is not None or fullname != "distutils": + return None + + return self.get_distutils_spec() + + def get_distutils_spec(self): + import importlib.util + + class DistutilsLoader(importlib.util.abc.Loader): + + def create_module(self, spec): + return importlib.import_module('._distutils', 'setuptools') + + def exec_module(self, module): + pass + + return importlib.util.spec_from_loader('distutils', DistutilsLoader()) + + +DISTUTILS_FINDER = DistutilsMetaFinder() + + +def add_shim(): + sys.meta_path.insert(0, DISTUTILS_FINDER) + + +def remove_shim(): + try: + sys.path.remove(DISTUTILS_FINDER) + except ValueError: + pass diff --git a/_distutils_hack/install.py b/_distutils_hack/install.py new file mode 100644 index 00000000..73f13b29 --- /dev/null +++ b/_distutils_hack/install.py @@ -0,0 +1,5 @@ +from . import enabled, add_shim + + +if enabled(): + add_shim() diff --git a/_distutils_hack/override.py b/_distutils_hack/override.py new file mode 100644 index 00000000..523139bb --- /dev/null +++ b/_distutils_hack/override.py @@ -0,0 +1,54 @@ +""" +Ensure that the local copy of distutils is preferred over stdlib. + +See https://github.com/pypa/setuptools/issues/417#issuecomment-392298401 +for more motivation. +""" + +import sys +import re +import importlib +import warnings + +from . import enabled + + +is_pypy = '__pypy__' in sys.builtin_module_names + + +def warn_distutils_present(): + if 'distutils' not in sys.modules: + return + if is_pypy and sys.version_info < (3, 7): + # PyPy for 3.6 unconditionally imports distutils, so bypass the warning + # https://foss.heptapod.net/pypy/pypy/-/blob/be829135bc0d758997b3566062999ee8b23872b4/lib-python/3/site.py#L250 + return + warnings.warn( + "Distutils was imported before Setuptools. This usage is discouraged " + "and may exhibit undesirable behaviors or errors. Please use " + "Setuptools' objects directly or at least import Setuptools first.") + + +def clear_distutils(): + if 'distutils' not in sys.modules: + return + warnings.warn("Setuptools is replacing distutils.") + mods = [name for name in sys.modules if re.match(r'distutils\b', name)] + for name in mods: + del sys.modules[name] + + +def ensure_local_distutils(): + clear_distutils() + distutils = importlib.import_module('setuptools._distutils') + distutils.__name__ = 'distutils' + sys.modules['distutils'] = distutils + + # sanity check that submodules load as expected + core = importlib.import_module('distutils.core') + assert '_distutils' in core.__file__, core.__file__ + + +warn_distutils_present() +if enabled(): + ensure_local_distutils() diff --git a/_distutils_importer/__init__.py b/_distutils_importer/__init__.py deleted file mode 100644 index 3ad70100..00000000 --- a/_distutils_importer/__init__.py +++ /dev/null @@ -1,45 +0,0 @@ -import sys -import os - - -def enabled(): - """ - Allow selection of distutils by environment variable. - """ - which = os.environ.get('SETUPTOOLS_USE_DISTUTILS', 'stdlib') - return which == 'local' - - -class DistutilsMetaFinder: - def find_spec(self, fullname, path, target=None): - if path is not None or fullname != "distutils": - return None - - return self.get_distutils_spec() - - def get_distutils_spec(self): - import importlib.util - - class DistutilsLoader(importlib.util.abc.Loader): - - def create_module(self, spec): - return importlib.import_module('._distutils', 'setuptools') - - def exec_module(self, module): - pass - - return importlib.util.spec_from_loader('distutils', DistutilsLoader()) - - -DISTUTILS_FINDER = DistutilsMetaFinder() - - -def add_shim(): - sys.meta_path.insert(0, DISTUTILS_FINDER) - - -def remove_shim(): - try: - sys.path.remove(DISTUTILS_FINDER) - except ValueError: - pass diff --git a/_distutils_importer/install.py b/_distutils_importer/install.py deleted file mode 100644 index 73f13b29..00000000 --- a/_distutils_importer/install.py +++ /dev/null @@ -1,5 +0,0 @@ -from . import enabled, add_shim - - -if enabled(): - add_shim() diff --git a/_distutils_importer/override.py b/_distutils_importer/override.py deleted file mode 100644 index 523139bb..00000000 --- a/_distutils_importer/override.py +++ /dev/null @@ -1,54 +0,0 @@ -""" -Ensure that the local copy of distutils is preferred over stdlib. - -See https://github.com/pypa/setuptools/issues/417#issuecomment-392298401 -for more motivation. -""" - -import sys -import re -import importlib -import warnings - -from . import enabled - - -is_pypy = '__pypy__' in sys.builtin_module_names - - -def warn_distutils_present(): - if 'distutils' not in sys.modules: - return - if is_pypy and sys.version_info < (3, 7): - # PyPy for 3.6 unconditionally imports distutils, so bypass the warning - # https://foss.heptapod.net/pypy/pypy/-/blob/be829135bc0d758997b3566062999ee8b23872b4/lib-python/3/site.py#L250 - return - warnings.warn( - "Distutils was imported before Setuptools. This usage is discouraged " - "and may exhibit undesirable behaviors or errors. Please use " - "Setuptools' objects directly or at least import Setuptools first.") - - -def clear_distutils(): - if 'distutils' not in sys.modules: - return - warnings.warn("Setuptools is replacing distutils.") - mods = [name for name in sys.modules if re.match(r'distutils\b', name)] - for name in mods: - del sys.modules[name] - - -def ensure_local_distutils(): - clear_distutils() - distutils = importlib.import_module('setuptools._distutils') - distutils.__name__ = 'distutils' - sys.modules['distutils'] = distutils - - # sanity check that submodules load as expected - core = importlib.import_module('distutils.core') - assert '_distutils' in core.__file__, core.__file__ - - -warn_distutils_present() -if enabled(): - ensure_local_distutils() diff --git a/conftest.py b/conftest.py index 868bf5be..25537f56 100644 --- a/conftest.py +++ b/conftest.py @@ -15,7 +15,7 @@ collect_ignore = [ 'tests/manual_test.py', 'setuptools/tests/mod_with_constant.py', 'setuptools/_distutils', - '_distutils_importer', + '_distutils_hack', ] diff --git a/setup.py b/setup.py index a6e1abc4..2d8bdf85 100755 --- a/setup.py +++ b/setup.py @@ -95,7 +95,7 @@ class install_with_pth(install): def initialize_options(self): install.initialize_options(self) self.extra_path = ( - 'distutils-precedence', 'import _distutils_importer.install') + 'distutils-precedence', 'import _distutils_hack.install') def finalize_options(self): install.finalize_options(self) diff --git a/setuptools/__init__.py b/setuptools/__init__.py index 80b287b4..99094230 100644 --- a/setuptools/__init__.py +++ b/setuptools/__init__.py @@ -5,7 +5,7 @@ import functools import os import re -import _distutils_importer.override # noqa: F401 +import _distutils_hack.override # noqa: F401 import distutils.core from distutils.errors import DistutilsOptionError diff --git a/setuptools/sandbox.py b/setuptools/sandbox.py index 342a713f..24a36080 100644 --- a/setuptools/sandbox.py +++ b/setuptools/sandbox.py @@ -200,7 +200,7 @@ _MODULES_TO_HIDE = { 'distutils', 'pkg_resources', 'Cython', - '_distutils_importer', + '_distutils_hack', } @@ -232,9 +232,9 @@ def hide_setuptools(): necessary to avoid issues such as #315 where setuptools upgrading itself would fail to find a function declared in the metadata. """ - _distutils_importer = sys.modules.get('_distutils_importer', None) - if _distutils_importer is not None: - _distutils_importer.remove_shim() + _distutils_hack = sys.modules.get('_distutils_hack', None) + if _distutils_hack is not None: + _distutils_hack.remove_shim() modules = filter(_needs_hiding, sys.modules) _clear_modules(modules) -- cgit v1.2.1