Skip to content

Commit

Permalink
Consolidate distutils importing hacks into _distutils_importer packag…
Browse files Browse the repository at this point in the history
…e. Generate distutils-precedence.pth inline.
  • Loading branch information
jaraco committed Jul 20, 2020
1 parent 85a0a90 commit e371422
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 30 deletions.
1 change: 0 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ include launcher.c
include msvc-build-launcher.cmd
include pytest.ini
include tox.ini
include distutils_precedence.pth
exclude pyproject.toml # Temporary workaround for #1644.
19 changes: 16 additions & 3 deletions _distutils_importer/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
"""
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 os


def enabled():
"""
Allow selection of distutils by environment variable.
"""
which = os.environ.get('SETUPTOOLS_USE_DISTUTILS', 'stdlib')
return which == 'local'


class DistutilsMetaFinder:
Expand Down Expand Up @@ -34,6 +50,3 @@ def remove_shim():
sys.path.remove(DISTUTILS_FINDER)
except ValueError:
pass


add_shim()
5 changes: 5 additions & 0 deletions _distutils_importer/install.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from . import enabled, add_shim


if enabled():
add_shim()
18 changes: 2 additions & 16 deletions setuptools/distutils_patch.py → _distutils_importer/override.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
"""
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 os
import importlib
import warnings

from . import enabled


is_pypy = '__pypy__' in sys.builtin_module_names

Expand All @@ -37,14 +31,6 @@ def clear_distutils():
del sys.modules[name]


def enabled():
"""
Allow selection of distutils by environment variable.
"""
which = os.environ.get('SETUPTOOLS_USE_DISTUTILS', 'stdlib')
return which == 'local'


def ensure_local_distutils():
clear_distutils()
distutils = importlib.import_module('setuptools._distutils')
Expand Down
2 changes: 1 addition & 1 deletion conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def pytest_addoption(parser):
'tests/manual_test.py',
'setuptools/tests/mod_with_constant.py',
'setuptools/_distutils',
'setuptools/distutils_patch.py',
'_distutils_importer',
]


Expand Down
1 change: 0 additions & 1 deletion distutils_precedence.pth

This file was deleted.

8 changes: 2 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,8 @@ class install_with_pth(install):

def initialize_options(self):
install.initialize_options(self)

name = 'distutils_precedence'
with open(os.path.join(here, name + '.pth'), 'rt') as f:
contents = f.read()

self.extra_path = (name, contents)
self.extra_path = (
'distutils-precedence', 'import _distutils_importer.install')

def finalize_options(self):
install.finalize_options(self)
Expand Down
3 changes: 1 addition & 2 deletions setuptools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import os
import re

# Disabled for now due to: #2228, #2230
import setuptools.distutils_patch # noqa: F401
import _distutils_importer.override # noqa: F401

import distutils.core
from distutils.errors import DistutilsOptionError
Expand Down

0 comments on commit e371422

Please sign in to comment.