10000 Fix an infinite loop when using `# fmt: on/off` ... (#3158) · IBMZ-Linux-OSS-Python/black@249c653 · GitHub
[go: up one dir, main page]

Skip to content

Commit 249c653

Browse files
yileiichard26JelleZijlstra
authored
Fix an infinite loop when using # fmt: on/off ... (psf#3158)
... in the middle of an expression or code block by adding a missing return. Co-authored-by: Richard Si <63936253+ichard26@users.noreply.github.com> Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
1 parent 6ea4edd commit 249c653

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
<!-- Changes that affect Black's stable style -->
1212

13+
- Fix an infinite loop when using `# fmt: on/off` in the middle of an expression or code
14+
block (#3158)
1315
- Fix incorrect handling of `# fmt: skip` on colon `:` lines. (#3148)
1416
- Comments are no longer deleted when a line had spaces removed around power operators
1517
(#2874)

src/black/comments.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from blib2to3.pgen2 import token
1414

1515
from black.nodes import first_leaf_column, preceding_leaf, container_of
16-
from black.nodes import STANDALONE_COMMENT, WHITESPACE
16+
from black.nodes import CLOSING_BRACKETS, STANDALONE_COMMENT, WHITESPACE
1717

1818
# types
1919
LN = Union[Leaf, Node]
@@ -227,6 +227,14 @@ def generate_ignored_nodes(
227227
# fix for fmt: on in children
228228
if contains_fmt_on_at_column(container, leaf.column, preview=preview):
229229
for child in container.children:
230+
if isinstance(child, Leaf) and is_fmt_on(child, preview=preview):
231+
if child.type in CLOSING_BRACKETS:
232+
# This means `# fmt: on` is placed at a different bracket level
233+
# than `# fmt: off`. This is an invalid use, but as a courtesy,
234+
# we include this closing bracket in the ignored nodes.
235+
# The alternative is to fail the formatting.
236+
yield child
237+
return
230238
if contains_fmt_on_at_column(child, leaf.column, preview=preview):
231239
return
232240
yield child

tests/data/simple_cases/fmtonoff5.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Regression test for https://github.com/psf/black/issues/3129.
2+
setup(
3+
entry_points={
4+
# fmt: off
5+
"console_scripts": [
6+
"foo-bar"
7+
"=foo.bar.:main",
8+
# fmt: on
9+
] # Includes an formatted indentation.
10+
},
11+
)
12+
13+
14+
# Regression test for https://github.com/psf/black/issues/2015.
15+
run(
16+
# fmt: off
17+
[
18+
"ls",
19+
"-la",
20+
]
21+
# fmt: on
22+
+ path,
23+
check=True,
24+
)
25+
26+
27+
# Regression test for https://github.com/psf/black/issues/3026.
28+
def test_func():
29+
# yapf: disable
30+
if unformatted( args ):
31+
return True
32+
# yapf: enable
33+
elif b:
34+
return True
35+
36+
return False

0 commit comments

Comments
 (0)
0