X-Git-Url: https://git.madduck.net/etc/vim.git/blobdiff_plain/728e5a2f1ed16e2cfe0ca5586edac6c10da436c5..23a00f051576d2e7edd18b6af382902cc34ea4a2:/black.py diff --git a/black.py b/black.py index 2783a10..35af598 100644 --- a/black.py +++ b/black.py @@ -607,6 +607,9 @@ def decode_bytes(src: bytes) -> Tuple[FileContent, Encoding, NewLine]: """ srcbuf = io.BytesIO(src) encoding, lines = tokenize.detect_encoding(srcbuf.readline) + if not lines: + return "", encoding, "\n" + newline = "\r\n" if b"\r\n" == lines[0][-2:] else "\n" srcbuf.seek(0) with io.TextIOWrapper(srcbuf, encoding) as tiow: @@ -623,7 +626,7 @@ GRAMMARS = [ def lib2to3_parse(src_txt: str) -> Node: """Given a string with source, return the lib2to3 Node.""" grammar = pygram.python_grammar_no_print_statement - if src_txt[-1] != "\n": + if src_txt[-1:] != "\n": src_txt += "\n" for grammar in GRAMMARS: drv = driver.Driver(grammar, pytree.convert) @@ -768,6 +771,7 @@ UNPACKING_PARENTS = { syms.dictsetmaker, syms.listmaker, syms.testlist_gexp, + syms.testlist_star_expr, } TEST_DESCENDANTS = { syms.test, @@ -2209,11 +2213,14 @@ def right_hand_split( result.append(leaf, preformatted=True) for comment_after in line.comments_after(leaf): result.append(comment_after, preformatted=True) - bracket_split_succeeded_or_raise(head, body, tail) assert opening_bracket and closing_bracket + body.should_explode = should_explode(body, opening_bracket) + bracket_split_succeeded_or_raise(head, body, tail) if ( + # the body shouldn't be exploded + not body.should_explode # the opening bracket is an optional paren - opening_bracket.type == token.LPAR + and opening_bracket.type == token.LPAR and not opening_bracket.value # the closing bracket is an optional paren and closing_bracket.type == token.RPAR @@ -2230,11 +2237,15 @@ def right_hand_split( yield from right_hand_split(line, line_length, py36=py36, omit=omit) return except CannotSplit: - pass + if len(body.leaves) == 1 and not is_line_short_enough( + body, line_length=line_length + ): + raise CannotSplit( + "Splitting failed, body is still too long and can't be split." + ) ensure_visible(opening_bracket) ensure_visible(closing_bracket) - body.should_explode = should_explode(body, opening_bracket) for result in (head, body, tail): if result: yield result