Skip to content

Commit 60417f3

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 bc37753 commit 60417f3

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
@@ -177,32 +177,20 @@ def _bootstrap(*, root=None, upgrade=False, user=False,
177177
if sys.implementation.cache_tag is None:
178178
args += ["--no-compile"]
179179

180+
if root:
181+
args += ["--root", root]
182+
180183
if user:
181184
# --user is mutually exclusive with --root/--prefix,
182185
# pip will enforce this.
183186
args += ["--user"]
184-
else:
185-
# Handle installation paths.
186-
# If --root is given but not --prefix, we default to a prefix of "/"
187-
# so that the install happens at the root of the --root directory.
188-
# Otherwise, pip would use the configured sys.prefix, e.g.
189-
# /usr/local, and install into ${root}/usr/local/.
190-
effective_prefix = prefix
191-
if root and not prefix:
192-
effective_prefix = "/"
193-
194-
if root:
195-
args += ["--root", root]
196-
197-
if effective_prefix:
198-
args += ["--prefix", effective_prefix]
199-
200-
# Force the script shebang to point to the correct, final
201-
# executable path. This is necessary when --root is used.
202-
executable_path = (
203-
Path(effective_prefix) / "bin" / Path(sys.executable).name
204-
)
205-
args += ["--executable", os.fsdecode(executable_path)]
187+
elif prefix:
188+
args += ["--prefix", prefix]
189+
190+
# Force the script shebang to point to the correct, final
191+
# executable path. This is necessary when --root is used.
192+
executable_path = Path(prefix) / "bin" / Path(sys.executable).name
193+
args += ["--executable", os.fsdecode(executable_path)]
206194

207195
return _run_pip([*args, "pip"], [os.fsdecode(tmp_wheel_path)])
208196

Lib/test/test_ensurepip.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,7 @@ def test_bootstrapping_with_root(self):
115115
self.run_pip.assert_called_once_with(
116116
[
117117
"install", "--no-cache-dir", "--no-index", "--find-links",
118-
unittest.mock.ANY, "--root", "/foo/bar/", "--prefix", "/",
119-
"--executable", unittest.mock.ANY, *COMPILE_OPT,
120-
"pip",
118+
unittest.mock.ANY, "--root", "/foo/bar/", "pip", *COMPILE_OPT,
121119
],
122120
unittest.mock.ANY,
123121
)
@@ -133,6 +131,18 @@ def test_bootstrapping_with_prefix(self):
133131
unittest.mock.ANY,
134132
)
135133

134+
def test_bootstrapping_with_root_and_prefix(self):
135+
ensurepip.bootstrap(root="/foo/root/", prefix="/foo/prefix/")
136+
self.run_pip.assert_called_once_with(
137+
[
138+
"install", "--no-cache-dir", "--no-index", "--find-links",
139+
unittest.mock.ANY, "--root", "/foo/root/",
140+
"--prefix", "/foo/prefix/", "--executable",
141+
unittest.mock.ANY, "pip",
142+
],
143+
unittest.mock.ANY,
144+
)
145+
136146
def test_bootstrapping_with_user(self):
137147
ensurepip.bootstrap(user=True)
138148

0 commit comments

Comments
 (0)