From 1f56503fe498aedb11d9e48d60d395045640a75f Mon Sep 17 00:00:00 2001 From: GiGaGon <107241144+MeGaGiGaGon@users.noreply.github.com> Date: Mon, 5 May 2025 22:47:10 -0700 Subject: [PATCH 1/5] Update comments.py --- src/black/comments.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/black/comments.py b/src/black/comments.py index 81d3cfd4a35..2b530f2b910 100644 --- a/src/black/comments.py +++ b/src/black/comments.py @@ -88,7 +88,7 @@ def list_comments(prefix: str, *, is_endmarker: bool) -> list[ProtoComment]: nlines = 0 ignored_lines = 0 form_feed = False - for index, full_line in enumerate(re.split("\r?\n", prefix)): + for index, full_line in enumerate(re.split("\r?\n|\r", prefix)): consumed += len(full_line) + 1 # adding the length of the split '\n' match = re.match(r"^(\s*)(\S.*|)$", full_line) assert match From b745e0e9a320104e64635e2d67eb3ee184f6dbb7 Mon Sep 17 00:00:00 2001 From: GiGaGon <107241144+MeGaGiGaGon@users.noreply.github.com> Date: Mon, 5 May 2025 22:54:51 -0700 Subject: [PATCH 2/5] Update CHANGES.md --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index 65bd285f452..cddf18b403b 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -16,6 +16,7 @@ - Fix crash when a tuple appears in the `as` clause of a `with` statement (#4634) - Fix crash when tuple is used as a context manager inside a `with` statement (#4646) +- Fix crash when formatting a `\` followed by a `\r` followed by a comment (#4663) ### Preview style From e3589b8212cf6f5596bf28eb8b20a60922138b42 Mon Sep 17 00:00:00 2001 From: GiGaGon <107241144+MeGaGiGaGon@users.noreply.github.com> Date: Mon, 5 May 2025 23:08:01 -0700 Subject: [PATCH 3/5] Update test_black.py --- tests/test_black.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/test_black.py b/tests/test_black.py index acafb521619..e471153d90a 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -2064,6 +2064,20 @@ def test_lines_with_leading_tabs_expanded(self) -> None: assert lines_with_leading_tabs_expanded("\t\tx") == [f"{tab}{tab}x"] assert lines_with_leading_tabs_expanded("\tx\n y") == [f"{tab}x", " y"] + def test_carrige_return_edge_cases(self) -> None: + # These tests are here instead of in the normal cases because + # of git's newline normalization and because it's hard to + # get `\r` vs `\r\n` vs `\n` to display properly + assert ( + black.format_str( + "try:\\\r# type: ignore\n pass\nfinally:\n pass\n", + mode=black.FileMode(), + ) + == "try: # type: ignore\n pass\nfinally:\n pass\n" + ) + assert black.format_str("{\r}", mode=black.FileMode()) == "{}\n" + assert black.format_str("pass #\r#\n", mode=black.FileMode()) == "pass #\n#\n" + class TestCaching: def test_get_cache_dir( From 628db07cd612897a51697ea9d407f78f5c2c3009 Mon Sep 17 00:00:00 2001 From: GiGaGon <107241144+MeGaGiGaGon@users.noreply.github.com> Date: Tue, 6 May 2025 08:19:55 -0700 Subject: [PATCH 4/5] Update tests/test_black.py Co-authored-by: Jelle Zijlstra --- tests/test_black.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_black.py b/tests/test_black.py index e471153d90a..7eee1e019b6 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -2064,7 +2064,7 @@ def test_lines_with_leading_tabs_expanded(self) -> None: assert lines_with_leading_tabs_expanded("\t\tx") == [f"{tab}{tab}x"] assert lines_with_leading_tabs_expanded("\tx\n y") == [f"{tab}x", " y"] - def test_carrige_return_edge_cases(self) -> None: + def test_carriage_return_edge_cases(self) -> None: # These tests are here instead of in the normal cases because # of git's newline normalization and because it's hard to # get `\r` vs `\r\n` vs `\n` to display properly From f8eea18b19a10595cad3683557001bd2e36ea5f5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 22 Jun 2025 17:31:26 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_black.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_black.py b/tests/test_black.py index 3c886a7a2cf..98d03652abe 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -2047,7 +2047,7 @@ def test_lines_with_leading_tabs_expanded(self) -> None: assert lines_with_leading_tabs_expanded("\tx") == [f"{tab}x"] assert lines_with_leading_tabs_expanded("\t\tx") == [f"{tab}{tab}x"] assert lines_with_leading_tabs_expanded("\tx\n y") == [f"{tab}x", " y"] - + def test_carriage_return_edge_cases(self) -> None: # These tests are here instead of in the normal cases because # of git's newline normalization and because it's hard to