Skip to content

Fix Mongolian ordinal IndexError on zero#661

Open
patchwright wants to merge 1 commit into
savoirfairelinux:masterfrom
patchwright:fix/mn-ordinal-zero
Open

Fix Mongolian ordinal IndexError on zero#661
patchwright wants to merge 1 commit into
savoirfairelinux:masterfrom
patchwright:fix/mn-ordinal-zero

Conversation

@patchwright

Copy link
Copy Markdown

Summary

num2words(0, lang='mn', to='ordinal') raises IndexError: string index out of range instead of returning an ordinal string.

Reproduction

from num2words import num2words

num2words(0, lang='mn', to='ordinal')     # IndexError
num2words(0, lang='mn', to='ordinal_num') # IndexError

Note to='ordinal' for other languages does not crash on zero, e.g. num2words(0, lang='en', to='ordinal') returns 'zeroth' — so this is an mn-specific gap.

Cause

In num2words/lang_MN.py, the ordinal suffix logic indexes the second-to-last character unconditionally:

elif number_str[-2] != '0':

For input 0, number_str is '0' (length 1), so [-2] is out of range.

Fix

Guard the index access:

-        elif number_str[-2] != '0':
+        elif len(number_str) > 1 and number_str[-2] != '0':

For zero, the condition short-circuits and the suffix falls through to the default 'дугаар' branch. That is the grammatically consistent outcome: 0 is not in the special set (1, 4, 9) that take distinct ordinal suffixes, so it receives the same default suffix as other non-special values (e.g. 10, 100).

Tests

Adds test_to_ordinal_zero and test_to_ordinal_num_zero, asserting num2words(0, lang='mn', to='ordinal') == 'тэг дугаар' and '0 дугаар' respectively. Both raise IndexError on the current code and pass with the fix.

num2words(0, lang='mn', to='ordinal') and to='ordinal_num' raised

IndexError because _get_ordinal_suffix indexed number_str[-2] on the

single-character string '0'. The base contract accepts 0 (English

returns 'zeroth'/'0th'; Dutch was fixed the same way in savoirfairelinux#365), so the

Mongolian module should honour it too. Guard the tens-digit branch with

a length check so 0 falls through to the default 'дугаар' suffix.
@patchwright

Copy link
Copy Markdown
Author

Gentle nudge on this one. The fix adds a length guard for deep negative indexing (x[-k] where k >= 2), which raises IndexError on short inputs. The test reproduces the crash on the current source and passes with the fix. Happy to adjust anything — just keeping it visible.

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