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.
Projectpython-babel/babel
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
| 33 | 33 | from functools import lru_cache | |
| 34 | 34 | from os.path import relpath | |
| 35 | 35 | 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 | |
| 37 | 37 | from typing import TYPE_CHECKING, Any | |
| 38 | 38 | ||
| 39 | 39 | from babel.messages._compat import find_entrypoints | |
| ⋯ | |||
| 530 | 530 | in_def = False | |
| 531 | 531 | continue | |
| 532 | 532 | if funcname: | |
| 533 | - | message_lineno = lineno | |
| 534 | 533 | call_stack += 1 | |
| 535 | 534 | elif in_def and tok == OP and value == ':': | |
| 536 | 535 | # End of a class definition without parens | |
| ⋯ | |||
| 580 | 579 | elif tok == STRING: | |
| 581 | 580 | val = _parse_python_string(value, encoding, future_flags) | |
| 582 | 581 | if val is not None: | |
| 582 | + | if not message_lineno: | |
| 583 | + | message_lineno = lineno | |
| 583 | 584 | buf.append(val) | |
| 584 | 585 | ||
| 585 | 586 | # Python 3.12+, see https://peps.python.org/pep-0701/#new-tokens | |
| 586 | 587 | elif tok == FSTRING_START: | |
| 587 | 588 | current_fstring_start = value | |
| 589 | + | if not message_lineno: | |
| 590 | + | message_lineno = lineno | |
| 588 | 591 | elif tok == FSTRING_MIDDLE: | |
| 589 | 592 | if current_fstring_start is not None: | |
| 590 | 593 | current_fstring_start += value | |
| ⋯ | |||
| 608 | 611 | # for the comment to still be a valid one | |
| 609 | 612 | old_lineno, old_comment = translator_comments.pop() | |
| 610 | 613 | translator_comments.append((old_lineno + 1, old_comment)) | |
| 614 | + | ||
| 615 | + | elif tok != NL and not message_lineno: | |
| 616 | + | message_lineno = lineno | |
| 611 | 617 | elif call_stack > 0 and tok == OP and value == ')': | |
| 612 | 618 | call_stack -= 1 | |
| 613 | 619 | elif funcname and call_stack == -1: | |
The check that tells the two apart
fail→pass·tests/messages/test_extract.py::ExtractPythonTestCase::test_comments_with_calls_that_spawn_multiple_lines
fail→pass·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.