Whole file

python-babel/babel

The author described this change as Fix extracted lineno with nested calls (#1126). It counts as a record because the checks below fail on the code as it stood at ea84d9d5f and pass on bef3a734f, with nothing else changed between the two runs.

Fix saved2024-10-19
Sharing licenceBSD-3-Clause · LICENSE
Change size+8 2

What the code was meant to do, written into the code itself as a save note

Fix extracted lineno with nested calls (#1126)

The change

3333 from functools import lru_cache
3434 from os.path import relpath
3535 from textwrap import dedent
36-from tokenize import COMMENT, NAME, OP, STRING, generate_tokens
36+from tokenize import COMMENT, NAME, NL, OP, STRING, generate_tokens
3737 from typing import TYPE_CHECKING, Any
3838
3939 from babel.messages._compat import find_entrypoints
530530 in_def = False
531531 continue
532532 if funcname:
533- message_lineno = lineno
534533 call_stack += 1
535534 elif in_def and tok == OP and value == ':':
536535 # End of a class definition without parens
580579 elif tok == STRING:
581580 val = _parse_python_string(value, encoding, future_flags)
582581 if val is not None:
582+ if not message_lineno:
583+ message_lineno = lineno
583584 buf.append(val)
584585
585586 # Python 3.12+, see https://peps.python.org/pep-0701/#new-tokens
586587 elif tok == FSTRING_START:
587588 current_fstring_start = value
589+ if not message_lineno:
590+ message_lineno = lineno
588591 elif tok == FSTRING_MIDDLE:
589592 if current_fstring_start is not None:
590593 current_fstring_start += value
608611 # for the comment to still be a valid one
609612 old_lineno, old_comment = translator_comments.pop()
610613 translator_comments.append((old_lineno + 1, old_comment))
614+
615+ elif tok != NL and not message_lineno:
616+ message_lineno = lineno
611617 elif call_stack > 0 and tok == OP and value == ')':
612618 call_stack -= 1
613619 elif funcname and call_stack == -1:

The check that tells the two apart

failpass·tests/messages/test_extract.py::ExtractPythonTestCase::test_comments_with_calls_that_spawn_multiple_lines
failpass·tests/messages/test_extract.py::ExtractPythonTestCase::test_nested_calls

Check file tests/messages/test_extract.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.

Origin and history

The code before itea84d9d5f0bd2859f3281e4dac812ae05d33f466
Broken version dated2024-10-19
Modulebabel.messages.extract
Units changedextract_python
Fingerprinta56f40ffd63c40ba
Checked2026-08-18 by goldset/0.1

Every field above is generated by our program. None of it is written by hand.

Other bugs found in python-babel/babel