From 55e2134a4ac896210d93ba7079cb70ace43f4f3b Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Thu, 23 Dec 2021 13:50:36 -0500 Subject: In distutils_hack, only add the metadata finder once. In ensure_local_distutils, rely on a context manager for reliable manipulation. Fixes #2958. --- _distutils_hack/__init__.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to '_distutils_hack/__init__.py') diff --git a/_distutils_hack/__init__.py b/_distutils_hack/__init__.py index 22bc9ed6..85a51370 100644 --- a/_distutils_hack/__init__.py +++ b/_distutils_hack/__init__.py @@ -3,6 +3,7 @@ import os import re import importlib import warnings +import contextlib is_pypy = '__pypy__' in sys.builtin_module_names @@ -52,9 +53,8 @@ def ensure_local_distutils(): # With the DistutilsMetaFinder in place, # perform an import to cause distutils to be # loaded from setuptools._distutils. Ref #2906. - add_shim() - importlib.import_module('distutils') - remove_shim() + with shim(): + importlib.import_module('distutils') # check that submodules load as expected core = importlib.import_module('distutils.core') @@ -129,6 +129,19 @@ class DistutilsMetaFinder: DISTUTILS_FINDER = DistutilsMetaFinder() +def ensure_shim(): + DISTUTILS_FINDER in sys.meta_path or add_shim() + + +@contextlib.contextmanager +def shim(): + add_shim() + try: + yield + finally: + remove_shim() + + def add_shim(): sys.meta_path.insert(0, DISTUTILS_FINDER) -- cgit v1.2.1 From 9c9c91c0a3951a7c521bd6eddc3aa5f84f0cfd9f Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Tue, 28 Dec 2021 14:34:37 -0500 Subject: Bypass distutils loader when setuptools module is no longer available on sys.path. Fixes #2980. --- _distutils_hack/__init__.py | 7 +++++++ 1 file changed, 7 insertions(+) (limited to '_distutils_hack/__init__.py') diff --git a/_distutils_hack/__init__.py b/_distutils_hack/__init__.py index 85a51370..55ea0825 100644 --- a/_distutils_hack/__init__.py +++ b/_distutils_hack/__init__.py @@ -86,6 +86,13 @@ class DistutilsMetaFinder: import importlib.abc import importlib.util + # In cases of path manipulation during sitecustomize, + # Setuptools might actually not be present even though + # the hook has been loaded. Allow the caller to fall + # back to stdlib behavior. See #2980. + if not importlib.util.find_spec('setuptools'): + return + class DistutilsLoader(importlib.abc.Loader): def create_module(self, spec): -- cgit v1.2.1