From 06d81cb32da88ec0b69d3bc01ad138a9c0d41520 Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Fri, 7 Jan 2022 00:30:26 +0100 Subject: [PATCH] Move filter into meta finder --- _distutils_hack/__init__.py | 41 +++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/_distutils_hack/__init__.py b/_distutils_hack/__init__.py index d324763d2c..0307734d88 100644 --- a/_distutils_hack/__init__.py +++ b/_distutils_hack/__init__.py @@ -1,31 +1,11 @@ # 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 @@ -33,6 +13,7 @@ def warn_distutils_present(): # 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 " @@ -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 @@ -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: @@ -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')