python-babel / babel

The official repository for Babel, the Python Internationalization Library
http://babel.pocoo.org/
BSD 3-Clause "New" or "Revised" License
1.34k stars 448 forks source link

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

Closed gabe-sherman closed 1 month ago

gabe-sherman commented 2 months ago

Addresses https://github.com/python-babel/babel/issues/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.

gabe-sherman commented 2 months ago

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

tomasr8 commented 2 months ago

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

gabe-sherman commented 2 months ago

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

gabe-sherman commented 2 months ago

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

tomasr8 commented 2 months ago

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 :slightly_smiling_face:

codecov[bot] commented 1 month ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

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

Additional details and impacted files ```diff @@ 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](https://app.codecov.io/gh/python-babel/babel/pull/1135/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-babel) | Coverage Δ | | |---|---|---| | [macos-12-3.10](https://app.codecov.io/gh/python-babel/babel/pull/1135/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-babel) | `90.08% <100.00%> (+0.01%)` | :arrow_up: | | [macos-12-3.11](https://app.codecov.io/gh/python-babel/babel/pull/1135/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-babel) | `90.01% <100.00%> (+0.01%)` | :arrow_up: | | [macos-12-3.12](https://app.codecov.io/gh/python-babel/babel/pull/1135/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-babel) | `90.23% <100.00%> (+0.05%)` | :arrow_up: | | [macos-12-3.13-dev](https://app.codecov.io/gh/python-babel/babel/pull/1135/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-babel) | `89.75% <100.00%> (+0.05%)` | :arrow_up: | | [macos-12-3.8](https://app.codecov.io/gh/python-babel/babel/pull/1135/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-babel) | `90.01% <100.00%> (+0.01%)` | :arrow_up: | | [macos-12-3.9](https://app.codecov.io/gh/python-babel/babel/pull/1135/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-babel) | `90.01% <100.00%> (+0.01%)` | :arrow_up: | | [macos-12-pypy3.10](https://app.codecov.io/gh/python-babel/babel/pull/1135/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-babel) | `90.08% <100.00%> (+0.01%)` | :arrow_up: | | [ubuntu-22.04-3.10](https://app.codecov.io/gh/python-babel/babel/pull/1135/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-babel) | `90.10% <100.00%> (+0.01%)` | :arrow_up: | | [ubuntu-22.04-3.11](https://app.codecov.io/gh/python-babel/babel/pull/1135/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-babel) | `90.03% <100.00%> (+0.01%)` | :arrow_up: | | [ubuntu-22.04-3.12](https://app.codecov.io/gh/python-babel/babel/pull/1135/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-babel) | `90.25% <100.00%> (+0.05%)` | :arrow_up: | | [ubuntu-22.04-3.13-dev](https://app.codecov.io/gh/python-babel/babel/pull/1135/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-babel) | `89.77% <100.00%> (+0.05%)` | :arrow_up: | | [ubuntu-22.04-3.8](https://app.codecov.io/gh/python-babel/babel/pull/1135/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-babel) | `90.03% <100.00%> (+0.01%)` | :arrow_up: | | [ubuntu-22.04-3.9](https://app.codecov.io/gh/python-babel/babel/pull/1135/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-babel) | `90.03% <100.00%> (+0.01%)` | :arrow_up: | | [ubuntu-22.04-pypy3.10](https://app.codecov.io/gh/python-babel/babel/pull/1135/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-babel) | `90.10% <100.00%> (+0.01%)` | :arrow_up: | | [windows-2022-3.10](https://app.codecov.io/gh/python-babel/babel/pull/1135/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-babel) | `90.22% <100.00%> (+0.01%)` | :arrow_up: | | [windows-2022-3.11](https://app.codecov.io/gh/python-babel/babel/pull/1135/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-babel) | `90.15% <100.00%> (+0.01%)` | :arrow_up: | | [windows-2022-3.12](https://app.codecov.io/gh/python-babel/babel/pull/1135/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-babel) | `90.37% <100.00%> (+0.05%)` | :arrow_up: | | [windows-2022-3.13-dev](https://app.codecov.io/gh/python-babel/babel/pull/1135/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-babel) | `89.89% <100.00%> (+0.05%)` | :arrow_up: | | [windows-2022-3.8](https://app.codecov.io/gh/python-babel/babel/pull/1135/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-babel) | `90.15% <100.00%> (+0.01%)` | :arrow_up: | | [windows-2022-3.9](https://app.codecov.io/gh/python-babel/babel/pull/1135/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-babel) | `90.15% <100.00%> (+0.01%)` | :arrow_up: | | [windows-2022-pypy3.10](https://app.codecov.io/gh/python-babel/babel/pull/1135/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-babel) | `90.22% <100.00%> (+0.01%)` | :arrow_up: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-babel#carryforward-flags-in-the-pull-request-comment) to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

akx commented 1 month ago

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.