Skip to content

Commit 77eeb77

Browse files
committed
When pip is imported, unload any existing distutils and disable the DistutilsMetaFinder.
1 parent be7a0a3 commit 77eeb77

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

_distutils_hack/__init__.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import re
44
import importlib
55
import warnings
6-
import inspect
76

87

98
is_pypy = '__pypy__' in sys.builtin_module_names
@@ -67,7 +66,9 @@ def do_override():
6766

6867
class DistutilsMetaFinder:
6968
def find_spec(self, fullname, path, target=None):
70-
if path is not None or fullname != "distutils" or self._bypass():
69+
self._disable_for_pip(fullname, path)
70+
71+
if path is not None or fullname != "distutils":
7172
return None
7273

7374
return self.get_distutils_spec()
@@ -85,15 +86,17 @@ def exec_module(self, module):
8586

8687
return importlib.util.spec_from_loader('distutils', DistutilsLoader())
8788

88-
def _bypass(self):
89+
def _disable_for_pip(self, fullname, path):
8990
"""
90-
Suppress the import of distutils from setuptools when running under pip.
91+
Ensure stdlib distutils when running under pip.
9192
See pypa/pip#8761 for rationale.
9293
"""
93-
return any(
94-
level.frame.f_globals['__name__'].startswith('pip.')
95-
for level in inspect.stack(context=False)
96-
)
94+
if path is not None or fullname != "pip":
95+
return
96+
97+
# pip is being imported the first time.
98+
clear_distutils()
99+
self.get_distutils_spec = lambda: None
97100

98101

99102
DISTUTILS_FINDER = DistutilsMetaFinder()

0 commit comments

Comments
 (0)