Repository navigation
Conversation
Six functions in humanize.number call float(value) just to test for NaN and
infinity before converting to int. For an int outside the float range that
conversion raises OverflowError, which isn't in the except clause, so it
escapes to the caller:
>>> humanize.apnumber(10**400)
OverflowError: int too large to convert to float
Same for intword, ordinal, metric, scientific and fractional. intcomma is
unaffected because it skips the float conversion when the value is an int.
Catch OverflowError alongside TypeError and ValueError and fall back to
str(value), which is what the docstrings already promise for values that
can't be converted. metric had no try block at all, so it gets one.
Member
This is the fourth PR for this issue. |
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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #419
Six functions in
humanize.numbercallfloat(value)only to check for NaN and infinity before converting toint. Anintoutside the float range raisesOverflowError, which isn't caught, so it reaches the caller:intword,ordinal,metric,scientificandfractionaldo the same.intcommais fine because it skips the float conversion when the value is already anint.Changes proposed in this pull request:
OverflowErroralongsideTypeErrorandValueError, falling back tostr(value)— what the docstrings already say happens to values that can't be converted.metricatryblock, since it calledmath.isfinite(value)with no guard at all.test_large_int_does_not_overflow, parametrized over the six functions.Without the change those six cases fail with
OverflowError; with it the suite is 752 passed, 132 skipped.