Skip to content

Raise NumOutOfRangeError instead of IndexError for out-of-range 'teen' magnitudes - #257

Open
eeshsaxena wants to merge 1 commit into
jaraco:mainfrom
eeshsaxena:fix-number-to-words-out-of-range
Open

Raise NumOutOfRangeError instead of IndexError for out-of-range 'teen' magnitudes#257
eeshsaxena wants to merge 1 commit into
jaraco:mainfrom
eeshsaxena:fix-number-to-words-out-of-range

Conversation

@eeshsaxena

Copy link
Copy Markdown

Fixes #242.

number_to_words() is documented to raise NumOutOfRangeError for out-of-range inputs, and millfn() enforces that:

def millfn(self, ind: int = 0) -> str:
    if ind > len(mill) - 1:
        raise NumOutOfRangeError
    return mill[ind]

But tenfn()'s tens == 1 (teen) branch indexed mill[mindex] directly, bypassing that check:

return f"{teen[units]}{mill[mindex]}"

So a number whose leading group is a teen — e.g. 10**40 — leaked an IndexError: list index out of range instead of NumOutOfRangeError. Numbers whose leading group is not a teen (e.g. 2 * 10**40) already raised NumOutOfRangeError via the other branch, so this makes the behaviour consistent.

Repro (before)

inflect.engine().number_to_words(10**40)   # IndexError: list index out of range

After

inflect.engine().number_to_words(10**40)   # NumOutOfRangeError

The fix routes the teen branch through millfn(). Added regression assertions to test_tenfn and a test_number_to_words_out_of_range covering 10**36, 10**40, 10**100. Full test_pwd.py: 172 passed, 16 xfailed.

tenfn() accessed mill[mindex] directly in the tens == 1 branch, bypassing
the bounds check in millfn(). number_to_words() on numbers whose leading
group is a teen (e.g. 10**40) therefore leaked an IndexError instead of
the documented NumOutOfRangeError. Route the lookup through millfn().

Fixes jaraco#242
@eeshsaxena eeshsaxena closed this Jul 16, 2026
@eeshsaxena eeshsaxena reopened this Jul 16, 2026
@eeshsaxena

Copy link
Copy Markdown
Author

Hi! Gentle ping on this one; it's been open a couple of weeks. Happy to rebase or make any changes if anything's needed; whenever you get a chance to take a look, I'd really appreciate it. Thanks!

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.

number_to_words leaks IndexError instead of NumOutOfRangeError for huge inputs

1 participant