Skip to content

Handle invalid plural index in msgstr[...] instead of raising ValueError (#1209)#1279

Open
vineethsaivs wants to merge 1 commit into
python-babel:masterfrom
vineethsaivs:fix/pofile-invalid-msgstr-index
Open

Handle invalid plural index in msgstr[...] instead of raising ValueError (#1209)#1279
vineethsaivs wants to merge 1 commit into
python-babel:masterfrom
vineethsaivs:fix/pofile-invalid-msgstr-index

Conversation

@vineethsaivs

Copy link
Copy Markdown

Fixes #1209.

read_po parses the plural index of a msgstr[N] line with int(idxarg[:-1]). When N is not an integer, this leaked a bare ValueError to the caller:

import io
from babel.messages.pofile import read_po
read_po(io.StringIO('msgstr[\x0c]'))
# ValueError: invalid literal for int() with base 10: '\x0c'

The fix routes a non-integer index through the parser's existing _invalid_pofile handling, so it raises a PoFileError when abort_invalid=True and otherwise warns and skips the line, consistent with how other malformed input is handled.

Added test_invalid_msgstr_index_issue_1209 (parametrized over abort_invalid), which fails on master (bare ValueError) and passes with this change.


Disclosure: prepared with AI assistance; reviewed by me and I can explain every line.

…eError

read_po parsed the plural index of a `msgstr[N]` line with `int(idxarg[:-1])`,
which leaked a bare `ValueError: invalid literal for int()` to the caller when N
was not an integer (e.g. `msgstr[\x0c]`).

Route a non-integer index through the parser's existing `_invalid_pofile`
handling, so it raises a `PoFileError` when `abort_invalid=True` and otherwise
warns and skips the line, consistent with other malformed input.

Fixes python-babel#1209

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.

Unhandled ValueError raised in read_po

1 participant