Skip to content

Commit

Permalink
Move filter into meta finder
Browse files Browse the repository at this point in the history
  • Loading branch information
tiran authored and jaraco committed Jan 8, 2022
1 parent 804b106 commit 06d81cb
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions _distutils_hack/__init__.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,19 @@
# don't import any costly modules
import sys
import os
import warnings


is_pypy = '__pypy__' in sys.builtin_module_names


class _TrivialRe:
def __init__(self, *patterns):
self._patterns = patterns

def match(self, string):
return all(pat in string for pat in self._patterns)


# warnings.filterwarnings() imports the re module
warnings._add_filter(
'ignore',
_TrivialRe("distutils", "deprecated"),
DeprecationWarning,
None,
0,
append=False
)


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
import warnings
warnings.warn(
"Distutils was imported before Setuptools, but importing Setuptools "
"also replaces the `distutils` module in `sys.modules`. This may lead "
Expand All @@ -45,6 +26,7 @@ def warn_distutils_present():
def clear_distutils():
if 'distutils' not in sys.modules:
return
import warnings
warnings.warn("Setuptools is replacing distutils.")
mods = [
name for name in sys.modules
Expand Down Expand Up @@ -89,6 +71,14 @@ def do_override():
ensure_local_distutils()


class _TrivialRe:
def __init__(self, *patterns):
self._patterns = patterns

def match(self, string):
return all(pat in string for pat in self._patterns)


class DistutilsMetaFinder:
def find_spec(self, fullname, path, target=None):
if path is not None:
Expand All @@ -102,6 +92,17 @@ def spec_for_distutils(self):
import importlib
import importlib.abc
import importlib.util
import warnings

# warnings.filterwarnings() imports the re module
warnings._add_filter(
'ignore',
_TrivialRe("distutils", "deprecated"),
DeprecationWarning,
None,
0,
append=True
)

try:
mod = importlib.import_module('setuptools._distutils')
Expand Down

0 comments on commit 06d81cb

Please sign in to comment.