[Python-checkins] (no subject)

Miro Hrončok webhook-mailer at python.org
Tue Mar 10 17:16:43 EDT 2020




To: python-checkins at python.org
Subject: bpo-38662: ensurepip invokes pip via runpy (GH-18901)
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
MIME-Version: 1.0

https://github.com/python/cpython/commit/88f82b2b9ea3514359cb6e3218121f753340=
63ac
commit: 88f82b2b9ea3514359cb6e3218121f75334063ac
branch: master
author: Miro Hron=C4=8Dok <miro at hroncok.cz>
committer: GitHub <noreply at github.com>
date: 2020-03-10T22:16:28+01:00
summary:

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 at gmail.com>
Co-Authored-By: Miro Hron=C4=8Dok <miro at hroncok.cz>

files:
A Misc/NEWS.d/next/Library/2020-03-10-15-32-31.bpo-38662.o1DMXj.rst
M Lib/ensurepip/__init__.py

diff --git a/Lib/ensurepip/__init__.py b/Lib/ensurepip/__init__.py
index 386ed6c25c763..545fce656fd6f 100644
--- a/Lib/ensurepip/__init__.py
+++ b/Lib/ensurepip/__init__.py
@@ -1,6 +1,7 @@
 import os
 import os.path
 import sys
+import runpy
 import tempfile
 from importlib import resources
=20
@@ -26,9 +27,18 @@ def _run_pip(args, additional_paths=3DNone):
     if additional_paths is not None:
         sys.path =3D additional_paths + sys.path
=20
-    # 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 =3D sys.argv[:]
+    sys.argv[1:] =3D args
+    try:
+        # run_module() alters sys.modules and sys.argv, but restores them at=
 exit
+        runpy.run_module("pip", run_name=3D"__main__", alter_sys=3DTrue)
+    except SystemExit as exc:
+        return exc.code
+    finally:
+        sys.argv[:] =3D backup_argv
+
+    raise SystemError("pip did not exit, this should never happen")
=20
=20
 def version():
diff --git a/Misc/NEWS.d/next/Library/2020-03-10-15-32-31.bpo-38662.o1DMXj.rs=
t b/Misc/NEWS.d/next/Library/2020-03-10-15-32-31.bpo-38662.o1DMXj.rst
new file mode 100644
index 0000000000000..241b2a6272ad6
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-03-10-15-32-31.bpo-38662.o1DMXj.rst
@@ -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.



More information about the Python-checkins mailing list