Skip to content

Commit c19e4f1

Browse files
committed
Handle non-finite values in naturalsize
1 parent 85bb7e7 commit c19e4f1

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/humanize/filesize.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
from __future__ import annotations
44

5-
from math import log
5+
from math import isfinite, log
66

77
from humanize.i18n import _gettext as _
8+
from humanize.number import _format_not_finite
89

910
suffixes = {
1011
"decimal": (
@@ -88,6 +89,8 @@ def naturalsize(
8889

8990
base = 1024 if (gnu or binary) else 1000
9091
bytes_ = float(value)
92+
if not isfinite(bytes_):
93+
return _format_not_finite(bytes_)
9194
abs_bytes = abs(bytes_)
9295

9396
if abs_bytes == 1 and not gnu:

tests/test_filesize.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,15 @@ def test_naturalsize(test_args: list[int] | list[int | bool], expected: str) ->
9393
test_args[0] = f"-{test_args[0]}"
9494

9595
assert humanize.naturalsize(*test_args) == "-" + expected
96+
97+
98+
@pytest.mark.parametrize(
99+
"value, expected",
100+
[
101+
(float("nan"), "NaN"),
102+
(float("inf"), "+Inf"),
103+
(float("-inf"), "-Inf"),
104+
],
105+
)
106+
def test_naturalsize_not_finite(value: float, expected: str) -> None:
107+
assert humanize.naturalsize(value) == expected

0 commit comments

Comments
 (0)