8000 bpo-38662: ensurepip invokes pip via runpy (GH-18901) · python/cpython@8d5c958 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8d5c958

Browse files
bpo-38662: ensurepip invokes pip via runpy (GH-18901)
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 <pradyunsg@gmail.com> Co-Authored-By: Miro Hrončok <miro@hroncok.cz> (cherry picked from commit 88f82b2) Co-authored-by: Miro Hrončok <miro@hroncok.cz>
1 parent f0fcf16 commit 8d5c958

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

Lib/ensurepip/__init__.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os.path
33
import pkgutil
44
import sys
5+
import runpy
56
import tempfile
67

78

@@ -23,9 +24,18 @@ def _run_pip(args, additional_paths=None):
2324
if additional_paths is not None:
2425
sys.path = additional_paths + sys.path
2526

26-
# Install the bundled software
27-
import pip._internal
28-
return pip._internal.main(args)
27+
# Invoke pip as if it's the main module, and catch the exit.
28+
backup_argv = sys.argv[:]
29+
sys.argv[1:] = args
30+
try:
31+
# run_module() alters sys.modules and sys.argv, but restores them at exit
32+
runpy.run_module("pip", run_name="__main__", alter_sys=True)
33+
except SystemExit as exc:
34+
return exc.code
35+
finally:
36+
sys.argv[:] = backup_argv
37+
38+
raise SystemError("pip did not exit, this should never happen")
2939

3040

3141
def version():
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
The ``ensurepip`` module now invokes ``pip`` via the ``runpy`` module.
2+
Hence it is no longer tightly coupled with the internal API of the bundled
3+
``pip`` version, allowing easier updates to a newer ``pip`` version both
4+
internally and for distributors.

0 commit comments

Comments
 (0)
0