ensure_local_distutils()
+class suppress(contextlib.suppress, contextlib.ContextDecorator):
+ """
+ A version of contextlib.suppress with decorator support.
+
+ >>> @suppress(KeyError)
+ ... def key_error():
+ ... {}['']
+ >>> key_error()
+ """
+
+
class DistutilsMetaFinder:
def find_spec(self, fullname, path, target=None):
if path is not None:
)
@classmethod
+ @suppress(AttributeError)
def is_get_pip(cls):
"""
Detect if get-pip is being invoked. Ref #2993.
if sys.version_info < (3, 7) else functools.partial(call, text=True)
+def win_sr(env):
+ """
+ On Windows, SYSTEMROOT must be present to avoid
+
+ > Fatal Python error: _Py_HashRandomization_Init: failed to
+ > get random numbers to initialize Python
+ """
+ if env is None:
+ return
+ if platform.system() == 'Windows':
+ env['SYSTEMROOT'] = os.environ['SYSTEMROOT']
+ return env
+
+
def find_distutils(venv, imports='distutils', env=None, **kwargs):
py_cmd = 'import {imports}; print(distutils.__file__)'.format(**locals())
cmd = ['python', '-c', py_cmd]
- if platform.system() == 'Windows':
- env['SYSTEMROOT'] = os.environ['SYSTEMROOT']
- return popen_text(venv.run)(cmd, env=env, **kwargs)
+ return popen_text(venv.run)(cmd, env=win_sr(env), **kwargs)
def count_meta_path(venv, env=None):
print(len(list(filter(is_distutils, sys.meta_path))))
""")
cmd = ['python', '-c', py_cmd]
- return int(popen_text(venv.run)(cmd, env=env))
+ return int(popen_text(venv.run)(cmd, env=win_sr(env)))
def test_distutils_stdlib(venv):
env = dict(SETUPTOOLS_USE_DISTUTILS='local')
assert venv.name in find_distutils(venv, env=env).split(os.sep)
assert count_meta_path(venv, env=env) <= 1
+
+
+def test_pip_import(venv):
+ """
+ Ensure pip can be imported.
+ Regression test for #3002.
+ """
+ cmd = ['python', '-c', 'import pip']
+ popen_text(venv.run)(cmd)