Repository navigation
Fix crash on backslash-only lines in code Black leaves as is - #5681
Merged
Merged
Conversation
A line holding only a backslash, right after another backslash continuation, wasn't made into an NL token, so the driver put a plain newline in the prefix where it was. fmt: off/skip code, lines outside --line-ranges and f-/t-string fields are written back from the prefix, so the backslash got dropped and the output was invalid, or a different string in an f-string field with `=` or a t-string field. Broken since 24.8.0 (psf#4343). Make it an NL token when it follows a backslash NL so it stays in the prefix. list_comments still counts it as an empty line, so comments don't move.
haeganm
force-pushed
the
fix-lone-backslash-in-verbatim
branch
from
November 10, 2026 03:11
a231434 to
14d18c0
Compare
Collaborator
|
Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
a line that's only a backslash, right after another backslash continuation, makes Black crash on code it's supposed to leave alone:
that gives
INTERNAL ERROR: Black produced invalid code. same thing on a# fmt: skipline and on lines outside--line-ranges. in an f-string field with=, or any t-string field, the field's source is part of the string, so it's "not equivalent to the source" instead, and with--fastBlack just writes the changed string. CPython's ownLib/test/test_tstring.pyhits it. it's a regression from 24.8.0, 24.4.2 handles all of these.since #4343,
transform_whitespaceonly turns the first backslash-newline into an NL token. the second one gets skipped, so the driver puts a plain newline in the prefix where the backslash was. regular code gets its whitespace redone so it doesn't matter there, but fmt: off/skip code and f-string fields are written straight from the prefix. now a backslash line that follows a backslash NL becomes an NL token too, so it stays in the prefix.list_commentsstill counts it as an empty line, so comments land where they did before.output only changes where Black used to crash or drop the backslash, so already formatted files come out the same.
a lone backslash right after a regular newline still turns into a blank line, same as before. handling that too would also fix
f"""{value/\/=}""", but it changes blank lines after comments, so I left it out.Checklist - did you ...
--previewstyle, following the stability policy?CHANGES.mdif necessary?