Skip to content

Commit 2d168e6

Browse files
authored
Merge pull request #12068 from pfmoore/python_option_subcommand
Error if the --python option is specified after the subcommand name
2 parents 72a32e9 + de8f0b5 commit 2d168e6

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

news/12067.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fail with an error if the ``--python`` option is specified after the subcommand name.

src/pip/_internal/cli/base_command.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,17 @@ def _main(self, args: List[str]) -> int:
131131
", ".join(sorted(always_enabled_features)),
132132
)
133133

134+
# Make sure that the --python argument isn't specified after the
135+
# subcommand. We can tell, because if --python was specified,
136+
# we should only reach this point if we're running in the created
137+
# subprocess, which has the _PIP_RUNNING_IN_SUBPROCESS environment
138+
# variable set.
139+
if options.python and "_PIP_RUNNING_IN_SUBPROCESS" not in os.environ:
140+
logger.critical(
141+
"The --python option must be placed before the pip subcommand name"
142+
)
143+
sys.exit(ERROR)
144+
134145
# TODO: Try to get these passing down from the command?
135146
# without resorting to os.environ to hold these.
136147
# This also affects isolated builds and it should.

tests/functional/test_install.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,7 @@ def test_install_nonlocal_compatible_wheel(
11571157
"--find-links",
11581158
data.find_links,
11591159
"--only-binary=:all:",
1160-
"--python",
1160+
"--python-version",
11611161
"3",
11621162
"--platform",
11631163
"fakeplat",
@@ -1177,7 +1177,7 @@ def test_install_nonlocal_compatible_wheel(
11771177
"--find-links",
11781178
data.find_links,
11791179
"--only-binary=:all:",
1180-
"--python",
1180+
"--python-version",
11811181
"3",
11821182
"--platform",
11831183
"fakeplat",

tests/functional/test_python_option.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,15 @@ def test_python_interpreter(
3939
script.pip("--python", env_path, "uninstall", "simplewheel", "--yes")
4040
result = script.pip("--python", env_path, "list", "--format=json")
4141
assert json.loads(result.stdout) == before
42+
43+
44+
def test_error_python_option_wrong_location(
45+
script: PipTestEnvironment,
46+
tmpdir: Path,
47+
shared_data: TestData,
48+
) -> None:
49+
env_path = os.fspath(tmpdir / "venv")
50+
env = EnvBuilder(with_pip=False)
51+
env.create(env_path)
52+
53+
script.pip("list", "--python", env_path, "--format=json", expect_error=True)

0 commit comments

Comments
 (0)