Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Lib/test/test_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,23 @@ def test_devnull(self):

self.assertTrue(os.path.exists(os.devnull))

def test_ensurepip_failure_shows_stderr(self):
"""Test that ensurepip failure output is shown to the user (gh-89830)"""
with tempfile.TemporaryDirectory() as env_dir:
builder = venv.EnvBuilder(with_pip=True)
# Simulate ensurepip failing with a traceback on stderr
def fake_call_new_python(context, *args, **kwargs):
raise subprocess.CalledProcessError(
returncode=1,
cmd=args,
stderr=b"AttributeError: simulated ensurepip failure"
)
with patch.object(venv.EnvBuilder, '_call_new_python', fake_call_new_python):
with captured_stderr() as err:
with self.assertRaises(subprocess.CalledProcessError):
builder.create(env_dir)
self.assertIn("simulated ensurepip failure", err.getvalue())

def do_test_with_pip(self, system_site_packages):
rmtree(self.env_dir)
with EnvironmentVarGuard() as envvars:
Expand Down
9 changes: 7 additions & 2 deletions Lib/venv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,13 @@ def _call_new_python(self, context, *py_args, **kwargs):

def _setup_pip(self, context):
"""Installs or upgrades pip in a virtual environment"""
self._call_new_python(context, '-m', 'ensurepip', '--upgrade',
'--default-pip', stderr=subprocess.STDOUT)
try:
self._call_new_python(context, '-m', 'ensurepip', '--upgrade',
'--default-pip', stderr=subprocess.PIPE)
except subprocess.CalledProcessError as e:
if e.stderr:
print(e.stderr.decode(errors='replace'), file=sys.stderr)
raise

def setup_scripts(self, context):
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix unhelpful error message when :mod:`venv` fails to install pip via
:mod:`ensurepip` by displaying the subprocess stderr output.
Loading