Skip to content

Commit

Permalink
Fix #10104: gettext: Duplicated locations are output to pot file
Browse files Browse the repository at this point in the history
When 3rd party extension does not provide line number for each message,
duplicated locations are output to pot file unexpectedly.  This filters
duplicated locations before generationg pot file.
  • Loading branch information
tk0miya committed May 22, 2022
1 parent 23ab36b commit 6853e58
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES
Expand Up @@ -16,6 +16,9 @@ Features added
Bugs fixed
----------

* #10104: gettext: Duplicated locations are shown if 3rd party extension does
not provide correct information

Testing
--------

Expand Down
2 changes: 1 addition & 1 deletion sphinx/builders/gettext.py
Expand Up @@ -57,7 +57,7 @@ def add(self, msg: str, origin: Union[Element, "MsgOrigin"]) -> None:

def __iter__(self) -> Generator[Message, None, None]:
for message in self.messages:
positions = [(source, line) for source, line, uuid in self.metadata[message]]
positions = sorted(set((source, line) for source, line, uuid in self.metadata[message]))
uuids = [uuid for source, line, uuid in self.metadata[message]]
yield Message(message, positions, uuids)

Expand Down
20 changes: 20 additions & 0 deletions tests/test_build_gettext.py
Expand Up @@ -8,9 +8,29 @@

import pytest

from sphinx.builders.gettext import Catalog, MsgOrigin
from sphinx.util.osutil import cd


def test_Catalog_duplicated_message():
catalog = Catalog()
catalog.add('hello', MsgOrigin('/path/to/filename', 1))
catalog.add('hello', MsgOrigin('/path/to/filename', 1))
catalog.add('hello', MsgOrigin('/path/to/filename', 2))
catalog.add('hello', MsgOrigin('/path/to/yetanother', 1))
catalog.add('world', MsgOrigin('/path/to/filename', 1))

assert len(list(catalog)) == 2

msg1, msg2 = list(catalog)
assert msg1.text == 'hello'
assert msg1.locations == [('/path/to/filename', 1),
('/path/to/filename', 2),
('/path/to/yetanother', 1)]
assert msg2.text == 'world'
assert msg2.locations == [('/path/to/filename', 1)]


@pytest.mark.sphinx('gettext', srcdir='root-gettext')
def test_build_gettext(app):
# Generic build; should fail only when the builder is horribly broken.
Expand Down

0 comments on commit 6853e58

Please sign in to comment.