mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-05 07:25:08 +00:00
fix(downloader): reject a speed limit too large for a float in parse_speed_limit
This commit is contained in:
@@ -160,7 +160,7 @@ full detail:
|
||||
| `track_progress` | list | Per-active-track `{label, progress, speed}` entries. |
|
||||
| `segments_done` | number | Segments downloaded for the track in progress. |
|
||||
| `segments_total` | number | Total segments for the track in progress. |
|
||||
| `speed` | string or `null` | Transfer speed of the track in progress, e.g. `4.20 MB/s`. `null` when no track downloads. |
|
||||
| `speed` | string or `null` | Transfer speed of the track in progress, e.g. `4.2 MB/s`. `null` when no track downloads. |
|
||||
| `skipped_subtitles` | list | Subtitles that were skipped as non-fatal failures (when `skip_subtitle_errors` is set). |
|
||||
| `skipped_titles` | list | Titles that the run skipped because a requested track or language is missing, each `{title, reason}`. |
|
||||
| `input_prompt` | string or `null` | The prompt the service waits on. Answer it with `POST /api/download/jobs/{job_id}/input`. |
|
||||
|
||||
@@ -45,7 +45,24 @@ def test_parse_speed_limit(value, expected):
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"value",
|
||||
["fast", "10TB", "10MiB", "-5M", "M", "1..5M", ".", "1.2.3", float("inf"), float("nan"), -5, -1.5],
|
||||
[
|
||||
"fast",
|
||||
"10TB",
|
||||
"10MiB",
|
||||
"-5M",
|
||||
"M",
|
||||
"1..5M",
|
||||
".",
|
||||
"1.2.3",
|
||||
float("inf"),
|
||||
float("nan"),
|
||||
-5,
|
||||
-1.5,
|
||||
pytest.param("9" * 400 + "k", id="400-digits-k"),
|
||||
pytest.param("9" * 400, id="400-digits"),
|
||||
pytest.param("9" * 308 + "g", id="308-digits-g"),
|
||||
pytest.param(10**400, id="int-10e400"),
|
||||
],
|
||||
)
|
||||
def test_parse_speed_limit_rejects(value):
|
||||
with pytest.raises(ValueError):
|
||||
|
||||
@@ -160,7 +160,7 @@ def parse_speed_limit(value: str | int | float | None) -> float | None:
|
||||
if value is None:
|
||||
return None
|
||||
if isinstance(value, (int, float)):
|
||||
if not math.isfinite(value) or value < 0:
|
||||
if not 0 <= value <= sys.float_info.max:
|
||||
raise ValueError(f"Invalid speed limit {value!r}: must be a positive number of bytes/sec.")
|
||||
return float(value) or None
|
||||
text = value.strip().lower().removesuffix("/s")
|
||||
@@ -169,7 +169,10 @@ def parse_speed_limit(value: str | int | float | None) -> float | None:
|
||||
match = re.fullmatch(r"(\d*\.?\d+)\s*([kmg]b?|b)?", text)
|
||||
if not match:
|
||||
raise ValueError(f"Invalid speed limit {value!r}: expected 500k, 5M, 1.5G, bytes per second or 'off'")
|
||||
return float(match.group(1)) * SPEED_UNITS[match.group(2) or ""]
|
||||
rate = float(match.group(1)) * SPEED_UNITS[match.group(2) or ""]
|
||||
if rate > sys.float_info.max:
|
||||
raise ValueError(f"Invalid speed limit {value!r}: too large")
|
||||
return rate
|
||||
|
||||
|
||||
def format_speed(bytes_per_sec: float) -> str:
|
||||
|
||||
Reference in New Issue
Block a user