Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-38662: Invoke pip via runpy, in ensurepip #18901

Merged
merged 4 commits into from
Mar 10, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 13 additions & 3 deletions Lib/ensurepip/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import os.path
import sys
import runpy
import tempfile
from importlib import resources

Expand All @@ -26,9 +27,18 @@ def _run_pip(args, additional_paths=None):
if additional_paths is not None:
sys.path = additional_paths + sys.path

# Install the bundled software
import pip._internal
return pip._internal.main(args)
# Invoke pip as if it's the main module, and catch the exit.
backup_argv = sys.argv[:]
sys.argv[1:] = args
try:
# run_module() alters sys.modules and sys.argv, but restore them at exit
hroncok marked this conversation as resolved.
Show resolved Hide resolved
runpy.run_module("pip", run_name="__main__", alter_sys=True)
hroncok marked this conversation as resolved.
Show resolved Hide resolved
except SystemExit as exc:
return exc.code
finally:
sys.argv[:] = backup_argv

raise SystemError("pip have not exited, that should never happen")
hroncok marked this conversation as resolved.
Show resolved Hide resolved
hroncok marked this conversation as resolved.
Show resolved Hide resolved


def version():
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
The ``ensurepip`` module now invokes ``pip`` via the ``runpy`` module.
Hence it is no longer tightly coupled with the internal API of the bundled
``pip`` version, allowing easier updates to a newer ``pip`` version both
internally and for distributors.