Skip to content

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

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

Merged
merged 18 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions babel/messages/pofile.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ def _add_message(self) -> None:

def _finish_current_message(self) -> None:
if self.messages:
if not self.translations:
self.translations.append([0, _NormalizedString("")])
self._invalid_pofile("", self.offset, f"missing msgstr for msgid '{self.messages[0].denormalize()}'")
self._add_message()

def _process_message_line(self, lineno, line, obsolete=False) -> None:
Expand Down
32 changes: 32 additions & 0 deletions tests/messages/test_pofile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1014,3 +1014,35 @@
"Language: \n"
''')
assert pofile.read_po(buf).locale is None

def test_issue_1134():
# creating multiple copies of IO objects so function calls do not effect later calls.
buf = StringIO('''
msgid "foo"
"''')

# Catalog is created with warning, no abort
output = pofile.read_po(buf)
assert isinstance(output, Catalog)
assert len(output._messages) == 1
assert output.messages["foo"].string == ''

Check failure on line 1028 in tests/messages/test_pofile.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 3.10)

test_issue_1134 AttributeError: 'Catalog' object has no attribute 'messages'. Did you mean: '_messages'?

Check failure on line 1028 in tests/messages/test_pofile.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 3.11)

test_issue_1134 AttributeError: 'Catalog' object has no attribute 'messages'

Check failure on line 1028 in tests/messages/test_pofile.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 3.11)

test_issue_1134 AttributeError: 'Catalog' object has no attribute 'messages'

Check failure on line 1028 in tests/messages/test_pofile.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 3.12)

test_issue_1134 AttributeError: 'Catalog' object has no attribute 'messages'. Did you mean: '_messages'?

Check failure on line 1028 in tests/messages/test_pofile.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 3.13-dev)

test_issue_1134 AttributeError: 'Catalog' object has no attribute 'messages'

buf = StringIO('''
msgid "foo"
"''')

# Catalog not created, aborted with PoFileError
with pytest.raises(pofile.PoFileError) as excinfo:
pofile.read_po(buf, abort_invalid=True)
assert str(excinfo.value) == "missing msgstr for msgid 'foo' on 1"

buf = StringIO('''
msgid "foo"
msgid_plural "foos"
"''')

# Catalog not created, aborted with PoFileError
with pytest.raises(pofile.PoFileError) as excinfo:
pofile.read_po(buf, abort_invalid=True)
assert str(excinfo.value) == "missing msgstr for msgid 'foo' on 1"

Loading