Repository: psf/black
File: black.py (Python)

Code before:
def format_file_in_place(
    src: Path,
    line_length: int,
    fast: bool,
    write_back: WriteBack = WriteBack.NO,
    mode: FileMode = FileMode.AUTO_DETECT,
    lock: Any = None,  # multiprocessing.Manager().Lock() is some crazy proxy
) -> bool:
    """Format file under `src` path. Return True if changed.

    If `write_back` is True, write reformatted code back to stdout.
    `line_length` and `fast` options are passed to :func:`format_file_contents`.
    """
    if src.suffix == ".pyi":
        mode |= FileMode.PYI
    with tokenize.open(src) as src_buffer:
        src_contents = src_buffer.read()
    try:
        dst_contents = format_file_contents(
            src_contents, line_length=line_length, fast=fast, mode=mode
        )
    except NothingChanged:
        return False

    if write_back == write_back.YES:
        with open(src, encoding=src_buffer.encoding, newline="") as src_buffer:
            newline = "\r\n" if "\r\n" in src_buffer.readline() else "\n"
        with open(src, "w", encoding=src_buffer.encoding, newline=newline) as f:
            f.write(dst_contents)
    elif write_back == write_back.DIFF:
        src_name = f"{src}  (original)"
        dst_name = f"{src}  (formatted)"
        diff_contents = diff(src_contents, dst_contents, src_name, dst_name)
        if lock:
            lock.acquire()
        try:
            sys.stdout.write(diff_contents)
        finally:
            if lock:
                lock.release()
    return True


def format_stdin_to_stdout(
    line_length: int,
    fast: bool,
    write_back: WriteBack = WriteBack.NO,
    mode: FileMode = FileMode.AUTO_DETECT,
) -> bool:
    """Format file on stdin. Return True if changed.


Review comment (performance):
You don't have to open it again, that's slow. You already have `src_contents` that you can check. Suggestion:
```py3
windows_newlines = src_contents.split("\n", 1)[0][-1:] == "\r"
newline = "\r\n" if windows_newlines else "\n"
```


Code after:

def format_file_in_place(
    src: Path,
    line_length: int,
    fast: bool,
    write_back: WriteBack = WriteBack.NO,
    mode: FileMode = FileMode.AUTO_DETECT,
    lock: Any = None,  # multiprocessing.Manager().Lock() is some crazy proxy
) -> bool:
    """Format file under `src` path. Return True if changed.

    If `write_back` is True, write reformatted code back to stdout.
    `line_length` and `fast` options are passed to :func:`format_file_contents`.
    """
    if src.suffix == ".pyi":
        mode |= FileMode.PYI

    with open(src, "rb") as buf:
        newline, encoding, src_contents = prepare_input(buf.read())
    try:
        dst_contents = format_file_contents(
            src_contents, line_length=line_length, fast=fast, mode=mode
        )
    except NothingChanged:
        return False

    if write_back == write_back.YES:
        with open(src, "w", encoding=encoding, newline=newline) as f:
            f.write(dst_contents)
    elif write_back == write_back.DIFF:
        src_name = f"{src}  (original)"
        dst_name = f"{src}  (formatted)"
        diff_contents = diff(src_contents, dst_contents, src_name, dst_name)
        if lock:
            lock.acquire()
        try:
            f = io.TextIOWrapper(
                sys.stdout.buffer,
                encoding=encoding,
                newline=newline,
                write_through=True,
            )
            f.write(diff_contents)
            f.detach()
        finally:
            if lock:
                lock.release()
    return True


def format_stdin_to_stdout(