[Python-checkins] gh-90355: Add isolated flag if currently isolated (GH-92857) (GH-94569)

ambv webhook-mailer at python.org
Tue Jul 5 11:58:40 EDT 2022


https://github.com/python/cpython/commit/922075c964e5d630402176169980b8831c42409c
commit: 922075c964e5d630402176169980b8831c42409c
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: ambv <lukasz at langa.pl>
date: 2022-07-05T17:58:28+02:00
summary:

gh-90355: Add isolated flag if currently isolated (GH-92857) (GH-94569)

Co-authored-by: Carter Dodd <carter.dodd at gmail.com>
Co-authored-by: Éric <merwok at netwok.org>
Co-authored-by: Łukasz Langa <lukasz at langa.pl>
(cherry picked from commit c8556bcf6c0b05ac46bd74880626a2853e7c99a1)

files:
A Misc/NEWS.d/next/Library/2022-01-03-15-07-06.bpo-46197.Z0djv6.rst
M Lib/ensurepip/__init__.py

diff --git a/Lib/ensurepip/__init__.py b/Lib/ensurepip/__init__.py
index 3fbe8b2a5b136..a82dbadce1635 100644
--- a/Lib/ensurepip/__init__.py
+++ b/Lib/ensurepip/__init__.py
@@ -90,8 +90,18 @@ def _run_pip(args, additional_paths=None):
 sys.argv[1:] = {args}
 runpy.run_module("pip", run_name="__main__", alter_sys=True)
 """
-    return subprocess.run([sys.executable, '-W', 'ignore::DeprecationWarning',
-                           "-c", code], check=True).returncode
+
+    cmd = [
+        sys.executable,
+        '-W',
+        'ignore::DeprecationWarning',
+        '-c',
+        code,
+    ]
+    if sys.flags.isolated:
+        # run code in isolated mode if currently running isolated
+        cmd.insert(1, '-I')
+    return subprocess.run(cmd, check=True).returncode
 
 
 def version():
diff --git a/Misc/NEWS.d/next/Library/2022-01-03-15-07-06.bpo-46197.Z0djv6.rst b/Misc/NEWS.d/next/Library/2022-01-03-15-07-06.bpo-46197.Z0djv6.rst
new file mode 100644
index 0000000000000..7a3b2d59dfaf4
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-01-03-15-07-06.bpo-46197.Z0djv6.rst
@@ -0,0 +1 @@
+Fix :mod:`ensurepip` environment isolation for subprocess running ``pip``.



More information about the Python-checkins mailing list