# Buggy code
def cmd_output_p(
    *cmd: str,
    retcode: Optional[int] = 0,
    **kwargs: Any,
) -> Tuple[int, bytes, Optional[bytes]]:
    assert retcode is None
    assert kwargs["stderr"] == subprocess.STDOUT, kwargs["stderr"]
    _setdefault_kwargs(kwargs)

    try:
        cmd = parse_shebang.normalize_cmd(cmd)
    except parse_shebang.ExecutableNotFoundError as e:
        return e.to_output()

    with open(os.devnull) as devnull, Pty() as pty:
        assert pty.r is not None
        kwargs.update({"stdin": devnull, "stdout": pty.w, "stderr": pty.w})
        proc = subprocess.Popen(cmd, **kwargs)
        pty.close_w()

        buf = b""
        while True:
            try:
                bts = os.read(pty.r, 4096)
            except OSError as e:
                if e.errno == errno.EIO:
                    bts = b""
                else:
                    raise
            else:
                buf += bts
            if not bts:
                break

    return proc.wait(), buf, None


# Traceback (OSError)
Traceback (most recent call last):
File "/[redacted]/venv/lib/python3.7/site-packages/pre_commit/error_handler.py", line 54, in error_handler
yield
File "/[redacted]/venv/lib/python3.7/site-packages/pre_commit/main.py", line 371, in main
return run(args.config, store, args)
File "/[redacted]/venv/lib/python3.7/site-packages/pre_commit/commands/run.py", line 339, in run
return _run_hooks(config, hooks, args, environ)
File "/[redacted]/venv/lib/python3.7/site-packages/pre_commit/commands/run.py", line 249, in _run_hooks
verbose=args.verbose, use_color=args.color,
File "/[redacted]/venv/lib/python3.7/site-packages/pre_commit/commands/run.py", line 165, in _run_single_hook
retcode, out = language.run_hook(hook, filenames, use_color)
File "/[redacted]/venv/lib/python3.7/site-packages/pre_commit/languages/script.py", line 19, in run_hook
return helpers.run_xargs(hook, cmd, file_args, color=color)
File "/[redacted]/venv/lib/python3.7/site-packages/pre_commit/languages/helpers.py", line 109, in run_xargs
return xargs(cmd, file_args, **kwargs)
File "/[redacted]/venv/lib/python3.7/site-packages/pre_commit/xargs.py", line 153, in xargs
for proc_retcode, proc_out, _ in results:
File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/_base.py", line 598, in result_iterator
yield fs.pop().result()
File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/_base.py", line 428, in result
return self.__get_result()
File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/_base.py", line 384, in __get_result
raise self._exception
File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/thread.py", line 57, in run
result = self.fn(*self.args, **self.kwargs)
File "/[redacted]/venv/lib/python3.7/site-packages/pre_commit/xargs.py", line 146, in run_cmd_partition
*run_cmd, retcode=None, stderr=subprocess.STDOUT, **kwargs,
File "/[redacted]/venv/lib/python3.7/site-packages/pre_commit/util.py", line 208, in cmd_output_p
proc = subprocess.Popen(cmd, **kwargs)
File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/subprocess.py", line 800, in __init__
restore_signals, start_new_session)
File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/subprocess.py", line 1551, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
OSError: [Errno 8] Exec format error: '/[redacted]/foo.py'

# Fixed code
def cmd_output_p(
    *cmd: str,
    retcode: Optional[int] = 0,
    **kwargs: Any,
) -> Tuple[int, bytes, Optional[bytes]]:
    assert retcode is None
    assert kwargs["stderr"] == subprocess.STDOUT, kwargs["stderr"]
    _setdefault_kwargs(kwargs)

    try:
        cmd = parse_shebang.normalize_cmd(cmd)
    except parse_shebang.ExecutableNotFoundError as e:
        return e.to_output()

    with open(os.devnull) as devnull, Pty() as pty:
        assert pty.r is not None
        kwargs.update({"stdin": devnull, "stdout": pty.w, "stderr": pty.w})
        try:
            proc = subprocess.Popen(cmd, **kwargs)
        except OSError as e:
            return _oserror_to_output(e)

        pty.close_w()

        buf = b""
        while True:
            try:
                bts = os.read(pty.r, 4096)
            except OSError as e:
                if e.errno == errno.EIO:
                    bts = b""
                else:
                    raise
            else:
                buf += bts
            if not bts:
                break

    return proc.wait(), buf, None