Skip to content

Commit 56d6efb

Browse files
committed
pythongh-75229: limit ensurepip prefix overrides to explicit requests
Keep root-only installs using the interpreter's default prefix while preserving the cross-build fix for Makefile-driven installs that pass an explicit prefix.
1 parent 8e928a6 commit 56d6efb

2 files changed

Lines changed: 23 additions & 25 deletions

File tree

Lib/ensurepip/__init__.py

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -165,32 +165,20 @@ def _bootstrap(*, root=None, upgrade=False, user=False,
165165
if verbosity:
166166
args += ["-" + "v" * verbosity]
167167

168+
if root:
169+
args += ["--root", root]
170+
168171
if user:
169172
# --user is mutually exclusive with --root/--prefix,
170173
# pip will enforce this.
171174
args += ["--user"]
172-
else:
173-
# Handle installation paths.
174-
# If --root is given but not --prefix, we default to a prefix of "/"
175-
# so that the install happens at the root of the --root directory.
176-
# Otherwise, pip would use the configured sys.prefix, e.g.
177-
# /usr/local, and install into ${root}/usr/local/.
178-
effective_prefix = prefix
179-
if root and not prefix:
180-
effective_prefix = "/"
181-
182-
if root:
183-
args += ["--root", root]
184-
185-
if effective_prefix:
186-
args += ["--prefix", effective_prefix]
187-
188-
# Force the script shebang to point to the correct, final
189-
# executable path. This is necessary when --root is used.
190-
executable_path = (
191-
Path(effective_prefix) / "bin" / Path(sys.executable).name
192-
)
193-
args += ["--executable", os.fsdecode(executable_path)]
175+
elif prefix:
176+
args += ["--prefix", prefix]
177+
178+
# Force the script shebang to point to the correct, final
179+
# executable path. This is necessary when --root is used.
180+
executable_path = Path(prefix) / "bin" / Path(sys.executable).name
181+
args += ["--executable", os.fsdecode(executable_path)]
194182

195183
return _run_pip([*args, "pip"], [os.fsdecode(tmp_wheel_path)])
196184

Lib/test/test_ensurepip.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,7 @@ def test_bootstrapping_with_root(self):
9494
self.run_pip.assert_called_once_with(
9595
[
9696
"install", "--no-cache-dir", "--no-index", "--find-links",
97-
unittest.mock.ANY, "--root", "/foo/bar/", "--prefix", "/",
98-
"--executable", unittest.mock.ANY,
99-
"pip",
97+
unittest.mock.ANY, "--root", "/foo/bar/", "pip",
10098
],
10199
unittest.mock.ANY,
102100
)
@@ -112,6 +110,18 @@ def test_bootstrapping_with_prefix(self):
112110
unittest.mock.ANY,
113111
)
114112

113+
def test_bootstrapping_with_root_and_prefix(self):
114+
ensurepip.bootstrap(root="/foo/root/", prefix="/foo/prefix/")
115+
self.run_pip.assert_called_once_with(
116+
[
117+
"install", "--no-cache-dir", "--no-index", "--find-links",
118+
unittest.mock.ANY, "--root", "/foo/root/",
119+
"--prefix", "/foo/prefix/", "--executable",
120+
unittest.mock.ANY, "pip",
121+
],
122+
unittest.mock.ANY,
123+
)
124+
115125
def test_bootstrapping_with_user(self):
116126
ensurepip.bootstrap(user=True)
117127

0 commit comments

Comments
 (0)