From 94ee9dbbab4c6c03bf4072d9fab9a4be73b22da0 Mon Sep 17 00:00:00 2001 From: imsurya26 <219236223+imsurya26@users.noreply.github.com> Date: Sat, 21 Nov 2026 21:20:43 +0530 Subject: [PATCH] Don't add a newline when normalizing long whitespace runs in PO values --- babel/messages/pofile.py | 4 +++- tests/messages/test_pofile.py | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/babel/messages/pofile.py b/babel/messages/pofile.py index 63401f10d..7e2f6be5d 100644 --- a/babel/messages/pofile.py +++ b/babel/messages/pofile.py @@ -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 = [] diff --git a/tests/messages/test_pofile.py b/tests/messages/test_pofile.py index 00e439b7c..76d634014 100644 --- a/tests/messages/test_pofile.py +++ b/tests/messages/test_pofile.py @@ -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"), [