From eff4aa5409a0dfe6ccd5ef4662578c77ee2954f1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C5=81ukasz=20Langa?= <lukasz@langa.pl>
Date: Tue, 5 Jul 2022 18:06:57 +0200
Subject: [PATCH] [3.9] gh-90355: Add isolated flag if currently isolated
 (GH-92857) (GH-94570)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Co-authored-by: Carter Dodd <carter.dodd@gmail.com>
Co-authored-by: Éric <merwok@netwok.org>
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
(cherry picked from commit c8556bcf6c0b05ac46bd74880626a2853e7c99a1)
---
 Lib/ensurepip/__init__.py                                  | 7 ++++++-
 .../next/Library/2022-01-03-15-07-06.bpo-46197.Z0djv6.rst  | 1 +
 2 files changed, 7 insertions(+), 1 deletion(-)
 create mode 100644 Misc/NEWS.d/next/Library/2022-01-03-15-07-06.bpo-46197.Z0djv6.rst

diff --git a/Lib/ensurepip/__init__.py b/Lib/ensurepip/__init__.py
index e510cc7fb2b..981534c4a09 100644
--- a/Lib/ensurepip/__init__.py
+++ b/Lib/ensurepip/__init__.py
@@ -31,7 +31,12 @@ 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, "-c", code], check=True).returncode
+
+    cmd = [sys.executable, '-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 00000000000..7a3b2d59dfa
--- /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``.
-- 
GitLab