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

Open
wants to merge 16 commits into
base: master
Choose a base branch
from

Conversation

gabe-sherman
Copy link

@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

babel/messages/pofile.py Outdated Show resolved Hide resolved
@@ -247,6 +247,9 @@ def _add_message(self) -> None:

def _finish_current_message(self) -> None:
if self.messages:
if not len(self.translations):
self.translations.append([0, _NormalizedString("")])
self._invalid_pofile("", self.offset, "invalid po file provided")
Copy link
Member

Choose a reason for hiding this comment

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

I think we can use a more descriptive error message here. Maybe something like Missing msgstr for msgid {msgid}?

@gabe-sherman
Copy link
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)

babel/messages/pofile.py Outdated Show resolved Hide resolved
tests/messages/test_pofile.py Outdated Show resolved Hide resolved
buf = StringIO('''msgid "this is an invalid po file"
msgstr "hello world"
msgid "msgid without str"''')
with pytest.raises(pofile.PoFileError):
Copy link
Member

Choose a reason for hiding this comment

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

PoFileError is raised for various reasons, I'd also assert that the exception message matches ours:

with pytest.raises(pofile.PoFileError) as excinfo:
    pofile.read_po(buf, abort_invalid=True)
assert str(excinfo.value) == "..." 

msgstr "hello world"
msgid "msgid without str"''')
with pytest.raises(pofile.PoFileError):
pofile.read_po(buf, abort_invalid=True)
Copy link
Member

Choose a reason for hiding this comment

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

I'd also add a test when abort_invalid is False and maybe one test when the input includes msgid_plural e.g.

msgid "foo"
msgid_plural "foos"

(Also, there's a missing newline at the end of this file ;))

@gabe-sherman
Copy link
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 :)

tests/messages/test_pofile.py Show resolved Hide resolved
tests/messages/test_pofile.py Outdated Show resolved Hide resolved
tests/messages/test_pofile.py Outdated Show resolved Hide resolved
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
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 🙂

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.

2 participants