Skip to content

Commit 5fe2dee

Browse files
committed
Include exception reason in pip version check warning
1 parent 439c091 commit 5fe2dee

File tree

5 files changed

+57
-4
lines changed

5 files changed

+57
-4
lines changed

news/13790.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve the error message from ``pip config set`` when ``PIP_CONFIG_FILE`` points to a non-regular file (e.g. ``/dev/null``).

src/pip/_internal/cli/index_command.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ def handle_pip_version_check(self, options: Values) -> None:
190190
)
191191
with session:
192192
_pip_self_version_check(session, options)
193-
except Exception:
194-
logger.warning("There was an error checking the latest version of pip.")
193+
except Exception as e:
194+
logger.warning(
195+
"There was an error checking the latest version of pip. (%s: %s)",
196+
e.__class__.__name__,
197+
e,
198+
)
195199
logger.debug("See below for error", exc_info=True)

src/pip/_internal/configuration.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,16 @@ def _get_parser_to_modify(self) -> tuple[str, RawConfigParser]:
378378
assert self.load_only
379379
parsers = self._parsers[self.load_only]
380380
if not parsers:
381-
# This should not happen if everything works correctly.
381+
env_config_file = os.environ.get("PIP_CONFIG_FILE")
382+
if env_config_file and not os.path.isfile(env_config_file):
383+
raise ConfigurationError(
384+
"Cannot modify pip configuration: "
385+
"PIP_CONFIG_FILE is not a regular file: "
386+
f"{env_config_file}"
387+
)
388+
382389
raise ConfigurationError(
383-
"Fatal Internal error [id=2]. Please report as a bug."
390+
"Cannot modify pip configuration: no configuration file is available."
384391
)
385392

386393
# Use the highest priority parser.

tests/functional/test_configuration.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,22 @@ def test_config_separated(
216216
result.stdout,
217217
)
218218

219+
def test_config_set_with_pip_config_file_devnull_shows_human_error(
220+
self, script: PipTestEnvironment
221+
) -> None:
222+
script.environ["PIP_CONFIG_FILE"] = os.devnull
223+
224+
result = script.pip(
225+
"config",
226+
"set",
227+
"global.index-url",
228+
"https://example.com/",
229+
expect_error=True,
230+
)
231+
assert "Fatal Internal error [id=2]" not in result.stderr
232+
assert "PIP_CONFIG_FILE" in result.stderr
233+
assert "not a regular file" in result.stderr
234+
219235
@pytest.mark.network
220236
def test_editable_mode_default_config(
221237
self, script: PipTestEnvironment, data: TestData

tests/unit/test_commands.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
import os
23
from typing import Callable
34
from unittest import mock
@@ -158,3 +159,27 @@ def test_list_pip_version_check(version_check_mock: mock.Mock, flag: str) -> Non
158159
version_check_mock.assert_called_once()
159160
else:
160161
version_check_mock.assert_not_called()
162+
163+
164+
@mock.patch("pip._internal.cli.index_command._pip_self_version_check")
165+
def test_handle_pip_version_check_warning_includes_reason(
166+
mock_version_check: mock.Mock, caplog: pytest.LogCaptureFixture
167+
) -> None:
168+
mock_version_check.side_effect = PermissionError("Access is denied")
169+
170+
cmd = create_command("install")
171+
options = cmd.parser.get_default_values()
172+
options.disable_pip_version_check = False
173+
options.no_index = False
174+
175+
with mock.patch.object(
176+
cmd,
177+
"_build_session",
178+
side_effect=PermissionError("Access is denied"),
179+
):
180+
with caplog.at_level(logging.WARNING):
181+
cmd.handle_pip_version_check(options)
182+
183+
assert "There was an error checking the latest version of pip." in caplog.text
184+
assert "PermissionError" in caplog.text
185+
assert "Access is denied" in caplog.text

0 commit comments

Comments
 (0)