From 46424a5096f8de47d2dd630a022e2fd8ab8b0424 Mon Sep 17 00:00:00 2001 From: Yin123-ybh <3388949919@qq.com> Date: Thu, 19 Nov 2026 15:51:34 +0800 Subject: [PATCH] Preserve translator comments for same-line Python calls --- babel/messages/extract.py | 8 +++++- docs/messages.rst | 8 ++++++ tests/messages/test_extract_python.py | 37 +++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/babel/messages/extract.py b/babel/messages/extract.py index 4676ee613..5281261f0 100644 --- a/babel/messages/extract.py +++ b/babel/messages/extract.py @@ -570,6 +570,7 @@ def extract_python( buf = [] messages = [] translator_comments = [] + comments_used_lineno = None in_def = in_translator_comments = False comment_tag = None @@ -585,6 +586,9 @@ def extract_python( current_fstring_lineno = 0 for tok, value, (lineno, _), _, _ in tokens: + if comments_used_lineno is not None and lineno > comments_used_lineno and not funcname: + translator_comments = [] + comments_used_lineno = None if not call_stack and tok == NAME and value in ('def', 'class'): in_def = True elif in_def and tok == OP and value == '(': @@ -638,12 +642,14 @@ def extract_python( [comment[1] for comment in translator_comments], ) + # Keep comments available to other translation calls on this line. + comments_used_lineno = call_stack[0] funcname = lineno = message_lineno = None call_stack.clear() messages = [] - translator_comments = [] in_translator_comments = False if nested: + translator_comments = [] funcname = value elif tok == STRING: val = _parse_python_string(value, encoding, future_flags) diff --git a/docs/messages.rst b/docs/messages.rst index c835d60a7..071ba708f 100644 --- a/docs/messages.rst +++ b/docs/messages.rst @@ -409,6 +409,14 @@ The resulting output in the catalog template would be something like:: msgid "Foo Bar" msgstr "" +In Python source, a translator comment can describe several sibling translation +calls on the next line. The following comment is included with both messages: + +.. code-block:: python + + # NOTE: SQL operators + operators = (_('IS'), _('NOT')) + Now, you might ask, why would I need that? Consider this simple case; you have a menu item called “manual”. You know what diff --git a/tests/messages/test_extract_python.py b/tests/messages/test_extract_python.py index be1cf970f..6173af0e2 100644 --- a/tests/messages/test_extract_python.py +++ b/tests/messages/test_extract_python.py @@ -273,6 +273,43 @@ def test_comment_tag_multiline(): assert messages[0][3] == ['NOTE: A translation comment', 'with a second line'] +@pytest.mark.parametrize('calls', [ + "messages = [_('IS'), _('NOT')]", + "_('IS'); _('NOT')", + "Symbol(key=_('IS'), args=[Symbol(key=_('NOT'))])", + "messages = [_('IS'), _(\n 'NOT'\n)]", +]) +@pytest.mark.parametrize('strip_comment_tags', [False, True]) +def test_comment_applies_to_calls_on_same_line(calls, strip_comment_tags): + source = f"# NOTE: SQL operators\n{calls}\n_('NEXT')\n" + messages = list(extract.extract( + 'python', BytesIO(source.encode()), comment_tags=['NOTE:'], + strip_comment_tags=strip_comment_tags, + )) + comments = ['SQL operators' if strip_comment_tags else 'NOTE: SQL operators'] + assert [(message, notes) for _, message, notes, _ in messages] == [ + ('IS', comments), + ('NOT', comments), + ('NEXT', []), + ] + + +def test_same_line_comments_do_not_leak_into_new_comment_block(): + source = BytesIO(b"""\ +# NOTE: first pair +_('ONE'); _('TWO') +# NOTE: second pair +_('THREE'); _('FOUR') +""") + messages = list(extract.extract('python', source, comment_tags=['NOTE:'])) + assert [(message, notes) for _, message, notes, _ in messages] == [ + ('ONE', ['NOTE: first pair']), + ('TWO', ['NOTE: first pair']), + ('THREE', ['NOTE: second pair']), + ('FOUR', ['NOTE: second pair']), + ] + + def test_translator_comments_with_previous_non_translator_comments(): buf = BytesIO(b""" # This shouldn't be in the output