FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix: catch OverflowError in naturalsize() for very large integers by koteshyelamati · Pull Request #355 · python-humanize/humanize · GitHub

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

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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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


Back | FazBrowse Home | New Git URL