Skip to content

Commit

Permalink
Merge pull request #12068 from pfmoore/python_option_subcommand
Browse files Browse the repository at this point in the history
Error if the --python option is specified after the subcommand name
  • Loading branch information
pfmoore committed Jun 8, 2023
2 parents 72a32e9 + de8f0b5 commit 2d168e6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions news/12067.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fail with an error if the ``--python`` option is specified after the subcommand name.
11 changes: 11 additions & 0 deletions src/pip/_internal/cli/base_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,17 @@ def _main(self, args: List[str]) -> int:
", ".join(sorted(always_enabled_features)),
)

# Make sure that the --python argument isn't specified after the
# subcommand. We can tell, because if --python was specified,
# we should only reach this point if we're running in the created
# subprocess, which has the _PIP_RUNNING_IN_SUBPROCESS environment
# variable set.
if options.python and "_PIP_RUNNING_IN_SUBPROCESS" not in os.environ:
logger.critical(
"The --python option must be placed before the pip subcommand name"
)
sys.exit(ERROR)

# TODO: Try to get these passing down from the command?
# without resorting to os.environ to hold these.
# This also affects isolated builds and it should.
Expand Down
4 changes: 2 additions & 2 deletions tests/functional/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,7 @@ def test_install_nonlocal_compatible_wheel(
"--find-links",
data.find_links,
"--only-binary=:all:",
"--python",
"--python-version",
"3",
"--platform",
"fakeplat",
Expand All @@ -1177,7 +1177,7 @@ def test_install_nonlocal_compatible_wheel(
"--find-links",
data.find_links,
"--only-binary=:all:",
"--python",
"--python-version",
"3",
"--platform",
"fakeplat",
Expand Down
12 changes: 12 additions & 0 deletions tests/functional/test_python_option.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,15 @@ def test_python_interpreter(
script.pip("--python", env_path, "uninstall", "simplewheel", "--yes")
result = script.pip("--python", env_path, "list", "--format=json")
assert json.loads(result.stdout) == before


def test_error_python_option_wrong_location(
script: PipTestEnvironment,
tmpdir: Path,
shared_data: TestData,
) -> None:
env_path = os.fspath(tmpdir / "venv")
env = EnvBuilder(with_pip=False)
env.create(env_path)

script.pip("list", "--python", env_path, "--format=json", expect_error=True)

0 comments on commit 2d168e6

Please sign in to comment.