Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix of list index out of range error in PoFileParser.add_message when translations is empty #1135

Merged
merged 18 commits into from
Oct 19, 2024

Conversation

gabe-sherman
Copy link
Contributor

@gabe-sherman gabe-sherman commented Sep 25, 2024

Addresses #1134

Previously, the add_message function would try to access the PoFileParser object's translations list without any checks. However, when an invalid po file is provided, this can lead to an index out of range error.

This PR adds a check to the _finish_current_message function and raises an invalid_pofile exception when the messages list is populated and translations is empty. This also adds a dummy translation to the translations list to avoid the index out of range error when an invalid_pofile exception is treated as a warning rather than raised.

Copy link
Member

@tomasr8 tomasr8 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I left two comments :)

edit: I'd also add a test for this case

@gabe-sherman
Copy link
Contributor Author

I updated the error message and added a test. Let me know if there's any other changes that should be made!

@tomasr8
Copy link
Member

tomasr8 commented Sep 27, 2024

Thanks! I'll take a look later today or tomorrow (ping me in case I forget)

gabe-sherman and others added 4 commits September 30, 2024 20:24
@gabe-sherman
Copy link
Contributor Author

Sorry for the delay, it's been a busy couple of days. I just added a couple more test cases!

Copy link
Member

@tomasr8 tomasr8 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good, just some test nitpicks :)

Comment on lines 1043 to 1045
with pytest.raises(pofile.PoFileError) as e:
pofile.read_po(buf, abort_invalid=True)
assert(str(e.value)) == "missing msgstr for msgid 'foo' on 1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing newline ;)

@gabe-sherman
Copy link
Contributor Author

Made those extra changes. Thanks for sticking with me as I work through everything!

@tomasr8
Copy link
Member

tomasr8 commented Oct 2, 2024

Made those extra changes. Thanks for sticking with me as I work through everything!

Thank you for working on the fix! It's looking good to me but I don't have commit rights in this repo so I'm pinging @akx 🙂

Copy link

codecov bot commented Oct 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.31%. Comparing base (f91754b) to head (2d72d66).
Report is 5 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1135      +/-   ##
==========================================
+ Coverage   91.26%   91.31%   +0.05%     
==========================================
  Files          27       27              
  Lines        4602     4618      +16     
==========================================
+ Hits         4200     4217      +17     
+ Misses        402      401       -1     
Flag Coverage Δ
macos-12-3.10 90.08% <100.00%> (+0.01%) ⬆️
macos-12-3.11 90.01% <100.00%> (+0.01%) ⬆️
macos-12-3.12 90.23% <100.00%> (+0.05%) ⬆️
macos-12-3.13-dev 89.75% <100.00%> (+0.05%) ⬆️
macos-12-3.8 90.01% <100.00%> (+0.01%) ⬆️
macos-12-3.9 90.01% <100.00%> (+0.01%) ⬆️
macos-12-pypy3.10 90.08% <100.00%> (+0.01%) ⬆️
ubuntu-22.04-3.10 90.10% <100.00%> (+0.01%) ⬆️
ubuntu-22.04-3.11 90.03% <100.00%> (+0.01%) ⬆️
ubuntu-22.04-3.12 90.25% <100.00%> (+0.05%) ⬆️
ubuntu-22.04-3.13-dev 89.77% <100.00%> (+0.05%) ⬆️
ubuntu-22.04-3.8 90.03% <100.00%> (+0.01%) ⬆️
ubuntu-22.04-3.9 90.03% <100.00%> (+0.01%) ⬆️
ubuntu-22.04-pypy3.10 90.10% <100.00%> (+0.01%) ⬆️
windows-2022-3.10 90.22% <100.00%> (+0.01%) ⬆️
windows-2022-3.11 90.15% <100.00%> (+0.01%) ⬆️
windows-2022-3.12 90.37% <100.00%> (+0.05%) ⬆️
windows-2022-3.13-dev 89.89% <100.00%> (+0.05%) ⬆️
windows-2022-3.8 90.15% <100.00%> (+0.01%) ⬆️
windows-2022-3.9 90.15% <100.00%> (+0.01%) ⬆️
windows-2022-pypy3.10 90.22% <100.00%> (+0.01%) ⬆️

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

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

@akx akx enabled auto-merge (squash) October 19, 2024 12:29
@akx
Copy link
Member

akx commented Oct 19, 2024

Thank you! The test was a bit buggy, so I went ahead and fixed that – and also that we don't do (a tiny bit of) extra work if we'd end up raising an error next anyway.

@akx akx merged commit f1c8633 into python-babel:master Oct 19, 2024
26 checks passed
@akx akx added this to the Babel 2.17 milestone Jan 14, 2025
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.

3 participants