From 15340a3f1ec40e4d7267518d04487d7b4108ee83 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Thu, 23 Dec 2021 11:45:22 -0800 Subject: distutils shim should ignore setuptools on another path --- _distutils_hack/__init__.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/_distutils_hack/__init__.py b/_distutils_hack/__init__.py index 22bc9ed6..a5c8e1cf 100644 --- a/_distutils_hack/__init__.py +++ b/_distutils_hack/__init__.py @@ -58,7 +58,9 @@ def ensure_local_distutils(): # check that submodules load as expected core = importlib.import_module('distutils.core') - assert '_distutils' in core.__file__, core.__file__ + # FIXME: this assertion blows up if the MetaFinder below has no-opped on a + # setuptools from another path + # assert '_distutils' in core.__file__, core.__file__ def do_override(): @@ -85,6 +87,15 @@ class DistutilsMetaFinder: def spec_for_distutils(self): import importlib.abc import importlib.util + from pathlib import Path + + st_mod = importlib.import_module('setuptools') + + # prevent this import redirection shim from interacting with a possibly + # incompatible setuptools in another location on sys.path + # see pypa/setuptools#2957 + if not Path(__file__).parents[1].samefile(Path(st_mod.__file__).parents[1]): + return None class DistutilsLoader(importlib.abc.Loader): -- cgit v1.2.1 From 41f61f3cce618b594c3c76b78162945f26fa6b61 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Wed, 29 Dec 2021 10:51:43 -0500 Subject: Restore assertion about expected distutils. --- _distutils_hack/__init__.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/_distutils_hack/__init__.py b/_distutils_hack/__init__.py index a5c8e1cf..cb694b34 100644 --- a/_distutils_hack/__init__.py +++ b/_distutils_hack/__init__.py @@ -58,9 +58,7 @@ def ensure_local_distutils(): # check that submodules load as expected core = importlib.import_module('distutils.core') - # FIXME: this assertion blows up if the MetaFinder below has no-opped on a - # setuptools from another path - # assert '_distutils' in core.__file__, core.__file__ + assert '_distutils' in core.__file__, core.__file__ def do_override(): -- cgit v1.2.1 From da8c68c49646d47b3946e882d402fa32131ade5d Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Wed, 29 Dec 2021 10:59:29 -0500 Subject: Check early for the presence of local distutils and bail out if the found version of Setuptools doesn't have distutils. --- _distutils_hack/__init__.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/_distutils_hack/__init__.py b/_distutils_hack/__init__.py index cb694b34..f64f9fea 100644 --- a/_distutils_hack/__init__.py +++ b/_distutils_hack/__init__.py @@ -85,20 +85,18 @@ class DistutilsMetaFinder: def spec_for_distutils(self): import importlib.abc import importlib.util - from pathlib import Path - st_mod = importlib.import_module('setuptools') - - # prevent this import redirection shim from interacting with a possibly - # incompatible setuptools in another location on sys.path - # see pypa/setuptools#2957 - if not Path(__file__).parents[1].samefile(Path(st_mod.__file__).parents[1]): + try: + mod = importlib.import_module('setuptools._distutils') + except Exception: + # an older Setuptools without a local distutils is taking + # precedence, so fall back to stdlib. Ref #2957. return None class DistutilsLoader(importlib.abc.Loader): def create_module(self, spec): - return importlib.import_module('setuptools._distutils') + return mod def exec_module(self, module): pass -- cgit v1.2.1 From 6eb6350caebbb36537722c7a0ec532b9ff98168f Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Wed, 29 Dec 2021 11:22:56 -0500 Subject: Update changelog. --- changelog.d/2962.misc.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/2962.misc.rst diff --git a/changelog.d/2962.misc.rst b/changelog.d/2962.misc.rst new file mode 100644 index 00000000..5a553a77 --- /dev/null +++ b/changelog.d/2962.misc.rst @@ -0,0 +1 @@ +Avoid attempting to use local distutils when the presiding version of Setuptools on the path doesn't have one. -- cgit v1.2.1