From 28db1b7ebd490cd16f750095f227a5659e845223 Mon Sep 17 00:00:00 2001 From: aDragon0707 Date: Mon, 7 Sep 2026 21:16:02 +0800 Subject: [PATCH 1/2] fix naturalsize ValueError for custom format strings with text (fix #366) --- src/humanize/filesize.py | 10 ++++++++-- tests/test_filesize.py | 7 +++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/humanize/filesize.py b/src/humanize/filesize.py index fb675fdc..40ba26b8 100644 --- a/src/humanize/filesize.py +++ b/src/humanize/filesize.py @@ -103,8 +103,14 @@ def naturalsize( # mantissa afterward; rounding can push it up to `base` (e.g. 999999 is # 999.999 kB, which formats to "1000.0 kB"). When that happens and a larger # suffix is available, step up one suffix so the result reads "1.0 MB". - if exp < len(suffix) and abs(float(format % (abs_bytes / (base**exp)))) >= base: - exp += 1 + if exp < len(suffix): + mantissa_text = format % (abs_bytes / (base**exp)) + try: + mantissa = float(mantissa_text) + except ValueError: + mantissa = None + if mantissa is not None and abs(mantissa) >= base: + exp += 1 space = "" if gnu else " " ret: str = format % (bytes_ / (base**exp)) + space + _(suffix[exp - 1]) return ret diff --git a/tests/test_filesize.py b/tests/test_filesize.py index e6956399..7ce0ccfe 100644 --- a/tests/test_filesize.py +++ b/tests/test_filesize.py @@ -103,3 +103,10 @@ def test_naturalsize(test_args: list[int] | list[int | bool], expected: str) -> test_args[0] = f"-{test_args[0]}" assert humanize.naturalsize(*test_args) == "-" + expected + + +def test_naturalsize_custom_format_with_text() -> None: + # regression: custom format strings with surrounding text must not crash + # https://github.com/python-humanize/humanize/issues/366 + assert humanize.naturalsize(999_999, gnu=True, format="Size: %.1f") == "Size: 976.6K" + assert humanize.naturalsize(999_999, gnu=True, format="%.1f bytes") == "976.6 bytesK" From 93e7b85e37a8b154dfa69a4a1d3936a31ed72908 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 7 Sep 2026 13:24:14 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_filesize.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/test_filesize.py b/tests/test_filesize.py index 7ce0ccfe..2e2bd516 100644 --- a/tests/test_filesize.py +++ b/tests/test_filesize.py @@ -108,5 +108,9 @@ def test_naturalsize(test_args: list[int] | list[int | bool], expected: str) -> def test_naturalsize_custom_format_with_text() -> None: # regression: custom format strings with surrounding text must not crash # https://github.com/python-humanize/humanize/issues/366 - assert humanize.naturalsize(999_999, gnu=True, format="Size: %.1f") == "Size: 976.6K" - assert humanize.naturalsize(999_999, gnu=True, format="%.1f bytes") == "976.6 bytesK" + assert ( + humanize.naturalsize(999_999, gnu=True, format="Size: %.1f") == "Size: 976.6K" + ) + assert ( + humanize.naturalsize(999_999, gnu=True, format="%.1f bytes") == "976.6 bytesK" + )