Skip to content

fix: catch OverflowError in naturalsize() for very large integers#355

Open
koteshyelamati wants to merge 1 commit into
python-humanize:mainfrom
koteshyelamati:patch-1
Open

fix: catch OverflowError in naturalsize() for very large integers#355
koteshyelamati wants to merge 1 commit into
python-humanize:mainfrom
koteshyelamati:patch-1

Conversation

@koteshyelamati

Copy link
Copy Markdown

Problem

naturalsize() calls float(value) unconditionally, which raises OverflowError for very large integers:

>>> import humanize
>>> humanize.naturalsize(10**309)
OverflowError: int too large to convert to float

It also raises a raw ValueError from float() for non-numeric strings, with a confusing message.

Fix

Wrap float(value) in try/except (ValueError, OverflowError) and re-raise with a clear message indicating the argument type.

After fix

>>> humanize.naturalsize(10**309)
ValueError: naturalsize() argument must be a number, not 'int'
>>> humanize.naturalsize("abc")
ValueError: naturalsize() argument must be a number, not 'str'

naturalsize(10**309) raises OverflowError because float(value) is called
unconditionally and int→float conversion overflows for very large integers.

Fix: wrap float(value) in try/except (ValueError, OverflowError) and re-raise
with a clear message.

Reproducer:
  >>> import humanize
  >>> humanize.naturalsize(10**309)
  OverflowError: int too large to convert to float
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant