Skip to content

Commit ad5d3dc

Browse files
committed
Use more descriptive exception when range requests are unsupported
1 parent 0872339 commit ad5d3dc

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

src/pip/_internal/network/lazy_wheel.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Lazy ZIP over HTTP"""
22

3-
__all__ = ['dist_from_wheel_url']
3+
__all__ = ['HTTPRangeRequestUnsupported', 'dist_from_wheel_url']
44

55
from bisect import bisect_left, bisect_right
66
from contextlib import contextmanager
@@ -23,13 +23,18 @@
2323
from pip._internal.network.session import PipSession
2424

2525

26+
class HTTPRangeRequestUnsupported(RuntimeError):
27+
pass
28+
29+
2630
def dist_from_wheel_url(name, url, session):
2731
# type: (str, str, PipSession) -> Distribution
2832
"""Return a pkg_resources.Distribution from the given wheel URL.
2933
3034
This uses HTTP range requests to only fetch the potion of the wheel
3135
containing metadata, just enough for the object to be constructed.
32-
If such requests are not supported, RuntimeError is raised.
36+
If such requests are not supported, HTTPRangeRequestUnsupported
37+
is raised.
3338
"""
3439
with LazyZipOverHTTP(url, session) as wheel:
3540
# For read-only ZIP files, ZipFile only needs methods read,
@@ -45,7 +50,8 @@ class LazyZipOverHTTP(object):
4550
4651
This uses HTTP range requests to lazily fetch the file's content,
4752
which is supposed to be fed to ZipFile. If such requests are not
48-
supported by the server, raise RuntimeError during initialization.
53+
supported by the server, raise HTTPRangeRequestUnsupported
54+
during initialization.
4955
"""
5056

5157
def __init__(self, url, session, chunk_size=CONTENT_CHUNK_SIZE):
@@ -60,7 +66,7 @@ def __init__(self, url, session, chunk_size=CONTENT_CHUNK_SIZE):
6066
self._left = [] # type: List[int]
6167
self._right = [] # type: List[int]
6268
if 'bytes' not in head.headers.get('Accept-Ranges', 'none'):
63-
raise RuntimeError('range request is not supported')
69+
raise HTTPRangeRequestUnsupported('range request is not supported')
6470
self._check_zip()
6571

6672
@property

src/pip/_internal/resolution/resolvelib/candidates.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import logging
22
import sys
33

4-
from pip._vendor.contextlib2 import suppress
54
from pip._vendor.packaging.specifiers import InvalidSpecifier, SpecifierSet
65
from pip._vendor.packaging.utils import canonicalize_name
76
from pip._vendor.packaging.version import Version
87

98
from pip._internal.exceptions import HashError, MetadataInconsistent
10-
from pip._internal.network.lazy_wheel import dist_from_wheel_url
9+
from pip._internal.network.lazy_wheel import (
10+
HTTPRangeRequestUnsupported,
11+
dist_from_wheel_url,
12+
)
1113
from pip._internal.req.constructors import (
1214
install_req_from_editable,
1315
install_req_from_line,
@@ -308,15 +310,22 @@ def iter_dependencies(self):
308310
assert self._name is not None
309311
logger.info('Collecting %s', self._ireq.req or self._ireq)
310312
# If RuntimeError is raised, fallback to self.dist.
311-
with indent_log(), suppress(RuntimeError):
313+
with indent_log():
312314
logger.info(
313315
'Obtaining dependency information from %s %s',
314316
self._name, self._version,
315317
)
316318
url = self._link.url.split('#', 1)[0]
317319
session = preparer.downloader._session
318-
dist = dist_from_wheel_url(self._name, url, session)
319-
self._check_metadata_consistency(dist)
320+
try:
321+
dist = dist_from_wheel_url(self._name, url, session)
322+
except HTTPRangeRequestUnsupported:
323+
logger.debug(
324+
'Failed to get dependency information '
325+
'using HTTP range requests from %s', url,
326+
)
327+
else:
328+
self._check_metadata_consistency(dist)
320329
return self._iter_dependencies(dist or self.dist)
321330

322331
def _prepare_abstract_distribution(self):

tests/unit/test_network_lazy_wheel.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
from pip._vendor.pkg_resources import Requirement
44
from pytest import fixture, mark, raises
55

6-
from pip._internal.network.lazy_wheel import dist_from_wheel_url
6+
from pip._internal.network.lazy_wheel import (
7+
HTTPRangeRequestUnsupported,
8+
dist_from_wheel_url,
9+
)
710
from pip._internal.network.session import PipSession
811
from tests.lib.requests_mocks import MockResponse
912

@@ -39,7 +42,7 @@ def test_dist_from_wheel_url(session):
3942
def test_dist_from_wheel_url_no_range(session, monkeypatch):
4043
"""Test handling when HTTP range requests are not supported."""
4144
monkeypatch.setattr(session, 'head', lambda *a, **kw: MockResponse(b''))
42-
with raises(RuntimeError):
45+
with raises(HTTPRangeRequestUnsupported):
4346
dist_from_wheel_url('mypy', MYPY_0_782_WHL, session)
4447

4548

0 commit comments

Comments
 (0)