Skip to content

Commit

Permalink
dev(narugo): raise error on every unexpected branches
Browse files Browse the repository at this point in the history
  • Loading branch information
narugo1992 committed Feb 6, 2025
1 parent 8312b8a commit 01dd37b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
4 changes: 1 addition & 3 deletions src/huggingface_hub/file_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,7 @@ def http_get(
initial_headers = headers
headers = copy.deepcopy(headers) or {}
if resume_size > 0:
adjusted_range = _adjust_range_header(headers.get("Range"), resume_size)
if adjusted_range is not None:
headers["Range"] = adjusted_range
headers["Range"] = _adjust_range_header(headers.get("Range"), resume_size)

r = _request_wrapper(
method="GET", url=url, stream=True, proxies=proxies, headers=headers, timeout=constants.HF_HUB_DOWNLOAD_TIMEOUT
Expand Down
14 changes: 7 additions & 7 deletions src/huggingface_hub/utils/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import threading
import time
import uuid
import warnings
from functools import lru_cache
from http import HTTPStatus
from shlex import quote
Expand Down Expand Up @@ -605,8 +604,7 @@ def _adjust_range_header(original_range: Optional[str], resume_size: int) -> Opt
return f"bytes={resume_size}-"

if "," in original_range:
warnings.warn(f"Multiple ranges detected - {original_range!r}, using full range after resume", UserWarning)
return f"bytes={resume_size}-"
raise ValueError(f"Multiple ranges detected - {original_range!r}, not supported yet.")

match = RANGE_REGEX.match(original_range)
if not match:
Expand All @@ -618,16 +616,18 @@ def _adjust_range_header(original_range: Optional[str], resume_size: int) -> Opt
raise RuntimeError(f"Invalid range format - {original_range!r}.")

new_suffix = int(end) - resume_size
new_range = f"bytes=-{new_suffix}"
if new_suffix <= 0:
return None
return f"bytes=-{new_suffix}"
raise RuntimeError(f"Empty new range - {new_range!r}.")
return new_range

start = int(start)
new_start = start + resume_size
if end:
end = int(end)
new_range = f"bytes={new_start}-{end}"
if new_start > end:
return None
return f"bytes={new_start}-{end}"
raise RuntimeError(f"Empty new range - {new_range!r}.")
return new_range

return f"bytes={new_start}-"
10 changes: 7 additions & 3 deletions tests/test_utils_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ def test_adjust_range_header():
assert _adjust_range_header(None, 10) == "bytes=10-"
assert _adjust_range_header("bytes=0-100", 10) == "bytes=10-100"
assert _adjust_range_header("bytes=-100", 10) == "bytes=-90"
assert _adjust_range_header("bytes=100-", 10) == "bytes=110-"

with pytest.raises(RuntimeError):
_adjust_range_header("invalid", 10)
Expand All @@ -327,8 +328,11 @@ def test_adjust_range_header():
_adjust_range_header("bytes=-", 10)

# Multiple ranges
assert _adjust_range_header("bytes=0-100,200-300", 10) == "bytes=10-"
with pytest.raises(ValueError):
_adjust_range_header("bytes=0-100,200-300", 10)

# Resume size exceeds range
assert _adjust_range_header("bytes=0-100", 150) is None
assert _adjust_range_header("bytes=-50", 100) is None
with pytest.raises(RuntimeError):
_adjust_range_header("bytes=0-100", 150)
with pytest.raises(RuntimeError):
_adjust_range_header("bytes=-50", 100)

0 comments on commit 01dd37b

Please sign in to comment.