Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error if the --python option is specified after the subcommand name #12068

Merged
merged 5 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)