Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion babel/messages/pofile.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,9 @@ def normalize(string: str, prefix: str = '', width: int = 76) -> str:
lines = []
for line in string.splitlines(True):
if len(escape(line)) + prefixlen > width:
chunks = WORD_SEP.split(line)
# Drop empty chunks: a trailing one would end up on a line of its own
# and be mistaken for the empty trailing line removed below.
chunks = [chunk for chunk in WORD_SEP.split(line) if chunk]
chunks.reverse()
while chunks:
buf = []
Expand Down
13 changes: 13 additions & 0 deletions tests/messages/test_pofile.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,19 @@ def test_denormalize_on_msgstr_without_empty_first_line():
assert expected_denormalized == pofile.denormalize(f'""\n{msgstr}')


@pytest.mark.parametrize(
'string',
[
'abc' + ' ' * 80,
'abc' + ' ' * 80 + '\n',
'abc\n' + '\t' * 40 + '\n',
],
)
def test_normalize_roundtrip_with_long_whitespace(string):
# A whitespace run wider than the line width must not add a newline
assert pofile.denormalize(pofile.normalize(string, width=76)) == string


@pytest.mark.parametrize(
("line", "locations"),
[
Expand Down