From f8008baaff77d7964d630e97cafc706ae1a2fc97 Mon Sep 17 00:00:00 2001 From: Pradyun Gedam Date: Fri, 1 Nov 2019 22:43:53 +0530 Subject: [PATCH 1/4] bpo-38662: Invoke pip via runpy, in ensurepip MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Co-Authored-By: Miro HronĨok --- Lib/ensurepip/__init__.py | 15 ++++++++++++--- .../2020-03-10-15-32-31.bpo-38662.o1DMXj.rst | 4 ++++ 2 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2020-03-10-15-32-31.bpo-38662.o1DMXj.rst diff --git a/Lib/ensurepip/__init__.py b/Lib/ensurepip/__init__.py index 386ed6c25c763e..6eb804676f2ba7 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 @@ -26,9 +27,17 @@ 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: + runpy.run_module("pip", run_name="__main__", alter_sys=True) + except SystemExit as e: + return e.code + finally: + sys.argv[:] = backup_argv + + raise SystemError("pip have not exited, that should never happen") def version(): diff --git a/Misc/NEWS.d/next/Library/2020-03-10-15-32-31.bpo-38662.o1DMXj.rst b/Misc/NEWS.d/next/Library/2020-03-10-15-32-31.bpo-38662.o1DMXj.rst new file mode 100644 index 00000000000000..241b2a6272ad60 --- /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. From c4b431a06e284506237e7ebb237e34da664f12a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miro=20Hron=C4=8Dok?= Date: Tue, 10 Mar 2020 15:52:12 +0100 Subject: [PATCH 2/4] Apply suggestions from code review Co-Authored-By: Victor Stinner --- Lib/ensurepip/__init__.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Lib/ensurepip/__init__.py b/Lib/ensurepip/__init__.py index 6eb804676f2ba7..7b4f8e472f98c9 100644 --- a/Lib/ensurepip/__init__.py +++ b/Lib/ensurepip/__init__.py @@ -31,9 +31,10 @@ def _run_pip(args, additional_paths=None): backup_argv = sys.argv[:] sys.argv[1:] = args try: + # run_module() alters sys.modules and sys.argv, but restore them at exit runpy.run_module("pip", run_name="__main__", alter_sys=True) - except SystemExit as e: - return e.code + except SystemExit as exc: + return exc.code finally: sys.argv[:] = backup_argv From 1109d5b595d47ebe39a252317524c5f6e63e5638 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miro=20Hron=C4=8Dok?= Date: Tue, 10 Mar 2020 15:53:01 +0100 Subject: [PATCH 3/4] Typo --- Lib/ensurepip/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/ensurepip/__init__.py b/Lib/ensurepip/__init__.py index 7b4f8e472f98c9..23c08b1b31054c 100644 --- a/Lib/ensurepip/__init__.py +++ b/Lib/ensurepip/__init__.py @@ -31,7 +31,7 @@ def _run_pip(args, additional_paths=None): backup_argv = sys.argv[:] sys.argv[1:] = args try: - # run_module() alters sys.modules and sys.argv, but restore them at exit + # 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 From 1db1b1d6301c1a806938daf87076bb511dfbb73b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miro=20Hron=C4=8Dok?= Date: Tue, 10 Mar 2020 21:09:12 +0100 Subject: [PATCH 4/4] Translate the exception message to English Co-Authored-By: Pradyun Gedam --- Lib/ensurepip/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/ensurepip/__init__.py b/Lib/ensurepip/__init__.py index 23c08b1b31054c..545fce656fd6f5 100644 --- a/Lib/ensurepip/__init__.py +++ b/Lib/ensurepip/__init__.py @@ -38,7 +38,7 @@ def _run_pip(args, additional_paths=None): finally: sys.argv[:] = backup_argv - raise SystemError("pip have not exited, that should never happen") + raise SystemError("pip did not exit, this should never happen") def version():