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
Contributor

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
@vineethsaivs
vineethsaivs force-pushed the fix/pofile-invalid-msgstr-index branch from 81d0a95 to ccfa1d9 Compare August 27, 2026 16:36
@codecov

codecov Bot commented Aug 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.37%. Comparing base (baf9431) to head (ccfa1d9).
⚠️ Report is 33 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1279      +/-   ##
==========================================
+ Coverage   92.05%   93.37%   +1.32%     
==========================================
  Files          27       27              
  Lines        4720     4923     +203     
==========================================
+ Hits         4345     4597     +252     
+ Misses        375      326      -49     
Flag Coverage Δ
macos-14-3.10 92.46% <100.00%> (+1.36%) ⬆️
macos-14-3.11 92.40% <100.00%> (+1.36%) ⬆️
macos-14-3.12 92.60% <100.00%> (+1.35%) ⬆️
macos-14-3.13 92.60% <100.00%> (+1.35%) ⬆️
macos-14-3.14 92.58% <100.00%> (+1.35%) ⬆️
macos-14-3.15 92.58% <100.00%> (?)
macos-14-3.8 92.33% <100.00%> (+1.36%) ⬆️
macos-14-3.9 92.39% <100.00%> (+1.36%) ⬆️
macos-14-pypy3.10 92.46% <100.00%> (+1.36%) ⬆️
ubuntu-24.04-3.10 92.48% <100.00%> (+1.36%) ⬆️
ubuntu-24.04-3.11 92.42% <100.00%> (+1.36%) ⬆️
ubuntu-24.04-3.12 92.62% <100.00%> (+1.35%) ⬆️
ubuntu-24.04-3.13 92.62% <100.00%> (+1.35%) ⬆️
ubuntu-24.04-3.14 92.60% <100.00%> (+1.35%) ⬆️
ubuntu-24.04-3.15 92.60% <100.00%> (?)
ubuntu-24.04-3.8 92.35% <100.00%> (+1.36%) ⬆️
ubuntu-24.04-3.9 92.41% <100.00%> (+1.36%) ⬆️
ubuntu-24.04-pypy3.10 92.48% <100.00%> (+1.36%) ⬆️
windows-2022-3.10 92.47% <100.00%> (+1.36%) ⬆️
windows-2022-3.11 92.41% <100.00%> (+1.36%) ⬆️
windows-2022-3.12 92.61% <100.00%> (+1.35%) ⬆️
windows-2022-3.13 92.61% <100.00%> (+1.35%) ⬆️
windows-2022-3.14 92.59% <100.00%> (+1.35%) ⬆️
windows-2022-3.15 92.59% <100.00%> (?)
windows-2022-3.8 92.45% <100.00%> (+1.36%) ⬆️
windows-2022-3.9 92.41% <100.00%> (+1.36%) ⬆️
windows-2022-pypy3.10 92.47% <100.00%> (+1.36%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@codspeed-hq

codspeed-hq Bot commented Aug 27, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 88 untouched benchmarks


Comparing vineethsaivs:fix/pofile-invalid-msgstr-index (ccfa1d9) with master (6ba6701)

Open in CodSpeed

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