Skip to content

Commit

Permalink
bpo-38662: ensurepip invokes pip via runpy (GH-18901)
Browse files Browse the repository at this point in the history
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.

This way, any changes to the internal pip API won't mean ensurepip needs to be
changed as well. Also, distributors can update their pip wheels independent on
CPython release schedule.

Co-Authored-By: Pradyun Gedam <[email protected]>
Co-Authored-By: Miro Hrončok <[email protected]>
(cherry picked from commit 88f82b2)

Co-authored-by: Miro Hrončok <[email protected]>
  • Loading branch information
miss-islington and hroncok authored Mar 10, 2020
1 parent b26ccfe commit 212acf5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
16 changes: 13 additions & 3 deletions Lib/ensurepip/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os.path
import pkgutil
import sys
import runpy
import tempfile


Expand All @@ -23,9 +24,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 restores them at exit
runpy.run_module("pip", run_name="__main__", alter_sys=True)
except SystemExit as exc:
return exc.code
finally:
sys.argv[:] = backup_argv

raise SystemError("pip did not exit, this should never happen")


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.

0 comments on commit 212acf5

Please sign in to comment.