Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 60d734a

Browse files
MeGaGiGaGonJelleZijlstracobaltt7pre-commit-ci[bot]
authored
Fix backslash carriage return comments (psf#4663)
* Update comments.py * Update CHANGES.md * Update test_black.py * Update tests/test_black.py Co-authored-by: Jelle Zijlstra <[email protected]> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: Jelle Zijlstra <[email protected]> Co-authored-by: cobalt <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 95bc569 commit 60d734a

3 files changed

Lines changed: 14 additions & 3 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- Handle `# fmt: skip` followed by a comment at the end of file (#4635)
1717
- Fix crash when a tuple appears in the `as` clause of a `with` statement (#4634)
1818
- Fix crash when tuple is used as a context manager inside a `with` statement (#4646)
19+
- Fix crash when formatting a `\` followed by a `\r` followed by a comment (#4663)
1920
- Fix crash on a `\\r\n` (#4673)
2021
- Fix crash on `await ...` (where `...` is a literal `Ellipsis`) (#4676)
2122
- Remove support for pre-python 3.7 `await/async` as soft keywords/variable names

src/black/comments.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def list_comments(prefix: str, *, is_endmarker: bool) -> list[ProtoComment]:
8888
nlines = 0
8989
ignored_lines = 0
9090
form_feed = False
91-
for index, full_line in enumerate(re.split("\r?\n", prefix)):
91+
for index, full_line in enumerate(re.split("\r?\n|\r", prefix)):
9292
consumed += len(full_line) + 1 # adding the length of the split '\n'
9393
match = re.match(r"^(\s*)(\S.*|)$", full_line)
9494
assert match

tests/test_black.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2048,10 +2048,20 @@ def test_lines_with_leading_tabs_expanded(self) -> None:
20482048
assert lines_with_leading_tabs_expanded("\t\tx") == [f"{tab}{tab}x"]
20492049
assert lines_with_leading_tabs_expanded("\tx\n y") == [f"{tab}x", " y"]
20502050

2051-
def test_backslash_carriage_return(self) -> None:
2051+
def test_carriage_return_edge_cases(self) -> None:
20522052
# These tests are here instead of in the normal cases because
20532053
# of git's newline normalization and because it's hard to
2054-
# get `\r` vs `\r\n` vs `\n` to display properly in editors
2054+
# get `\r` vs `\r\n` vs `\n` to display properly
2055+
assert (
2056+
black.format_str(
2057+
"try:\\\r# type: ignore\n pass\nfinally:\n pass\n",
2058+
mode=black.FileMode(),
2059+
)
2060+
== "try: # type: ignore\n pass\nfinally:\n pass\n"
2061+
)
2062+
assert black.format_str("{\r}", mode=black.FileMode()) == "{}\n"
2063+
assert black.format_str("pass #\r#\n", mode=black.FileMode()) == "pass #\n#\n"
2064+
20552065
assert black.format_str("x=\\\r\n1", mode=black.FileMode()) == "x = 1\n"
20562066
assert black.format_str("x=\\\n1", mode=black.FileMode()) == "x = 1\n"
20572067
assert black.format_str("x=\\\r1", mode=black.FileMode()) == "x = 1\n"

0 commit comments

Comments
 (0)