# Buggy code
def prepend_shebang_interpreter(args):
    # prepend interpreter directive (if any) to argument list
    #
    # When preparing virtual environments in a file container which has large
    # length, the system might not be able to invoke shebang scripts which
    # define interpreters beyond system limits (e.x. Linux as a limit of 128;
    # BINPRM_BUF_SIZE). This method can be used to check if the executable is
    # a script containing a shebang line. If so, extract the interpreter (and
    # possible optional argument) and prepend the values to the provided
    # argument list. tox will only attempt to read an interpreter directive of
    # a maximum size of 2048 bytes to limit excessive reading and support UNIX
    # systems which may support a longer interpret length.
    try:
        with open(args[0], "rb") as f:
            if f.read(1) == b"#" and f.read(1) == b"!":
                MAXINTERP = 2048
                interp = f.readline(MAXINTERP).rstrip()
                interp_args = interp.split(None, 1)[:2]
                return interp_args + args
    except IOError:
        pass
    return args


# Traceback (FileNotFoundError)
(.venv) [vlotorev@cygnus-pc infra-ci-scripts]$ export TOX_LIMITED_SHEBANG=1
(.venv) [vlotorev@cygnus-pc infra-ci-scripts]$ tox
ERROR: invocation failed (errno 2), args: [b'/homefs/vlotorev/projects/infra-ci-scripts/.tox/py34/bin/python3.4', '/homefs/vlotorev/projects/infra-ci-scripts/.tox/py34/bin/pip', 'freeze'], cwd: /homefs/vlotorev/projects/infra-ci-scripts
Traceback (most recent call last):
File "/homefs/vlotorev/projects/infra-ci-scripts/.venv/bin/tox", line 11, in <module>
sys.exit(cmdline())
File "/homefs/vlotorev/projects/infra-ci-scripts/.venv/lib64/python3.4/site-packages/tox/session.py", line 39, in cmdline
main(args)
File "/homefs/vlotorev/projects/infra-ci-scripts/.venv/lib64/python3.4/site-packages/tox/session.py", line 45, in main
retcode = Session(config).runcommand()
File "/homefs/vlotorev/projects/infra-ci-scripts/.venv/lib64/python3.4/site-packages/tox/session.py", line 422, in runcommand
return self.subcommand_test()
File "/homefs/vlotorev/projects/infra-ci-scripts/.venv/lib64/python3.4/site-packages/tox/session.py", line 622, in subcommand_test
self.runenvreport(venv)
File "/homefs/vlotorev/projects/infra-ci-scripts/.venv/lib64/python3.4/site-packages/tox/session.py", line 633, in runenvreport
packages = self.hook.tox_runenvreport(venv=venv, action=action)
File "/homefs/vlotorev/projects/infra-ci-scripts/.venv/lib64/python3.4/site-packages/pluggy/hooks.py", line 258, in __call__
return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
File "/homefs/vlotorev/projects/infra-ci-scripts/.venv/lib64/python3.4/site-packages/pluggy/manager.py", line 67, in _hookexec
return self._inner_hookexec(hook, methods, kwargs)
File "/homefs/vlotorev/projects/infra-ci-scripts/.venv/lib64/python3.4/site-packages/pluggy/manager.py", line 61, in <lambda>
firstresult=hook.spec_opts.get('firstresult'),
File "/homefs/vlotorev/projects/infra-ci-scripts/.venv/lib64/python3.4/site-packages/pluggy/callers.py", line 201, in _multicall
return outcome.get_result()
File "/homefs/vlotorev/projects/infra-ci-scripts/.venv/lib64/python3.4/site-packages/pluggy/callers.py", line 76, in get_result
raise ex[1].with_traceback(ex[2])
File "/homefs/vlotorev/projects/infra-ci-scripts/.venv/lib64/python3.4/site-packages/pluggy/callers.py", line 180, in _multicall
res = hook_impl.function(*args)
File "/homefs/vlotorev/projects/infra-ci-scripts/.venv/lib64/python3.4/site-packages/tox/venv.py", line 499, in tox_runenvreport
output = venv._pcall(args, cwd=venv.envconfig.config.toxinidir, action=action)
File "/homefs/vlotorev/projects/infra-ci-scripts/.venv/lib64/python3.4/site-packages/tox/venv.py", line 427, in _pcall
return action.popen(args, cwd=cwd, env=env, redirect=redirect, ignore_ret=ignore_ret)
File "/homefs/vlotorev/projects/infra-ci-scripts/.venv/lib64/python3.4/site-packages/tox/session.py", line 153, in popen
popen = self._popen(args, cwd, env=env, stdout=stdout, stderr=subprocess.STDOUT)
File "/homefs/vlotorev/projects/infra-ci-scripts/.venv/lib64/python3.4/site-packages/tox/session.py", line 248, in _popen
env=env,
File "/usr/lib64/python3.4/subprocess.py", line 856, in __init__
restore_signals, start_new_session)
File "/usr/lib64/python3.4/subprocess.py", line 1460, in _execute_child
raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: "b'/homefs/vlotorev/projects/infra-ci-scripts/.tox/py34/bin/python3.4'"

# Fixed code
def prepend_shebang_interpreter(args):
    # prepend interpreter directive (if any) to argument list
    #
    # When preparing virtual environments in a file container which has large
    # length, the system might not be able to invoke shebang scripts which
    # define interpreters beyond system limits (e.x. Linux as a limit of 128;
    # BINPRM_BUF_SIZE). This method can be used to check if the executable is
    # a script containing a shebang line. If so, extract the interpreter (and
    # possible optional argument) and prepend the values to the provided
    # argument list. tox will only attempt to read an interpreter directive of
    # a maximum size of 2048 bytes to limit excessive reading and support UNIX
    # systems which may support a longer interpret length.
    try:
        with open(args[0], "rb") as f:
            if f.read(1) == b"#" and f.read(1) == b"!":
                MAXINTERP = 2048
                interp = f.readline(MAXINTERP).rstrip().decode("UTF-8")
                interp_args = interp.split(None, 1)[:2]
                return interp_args + args
    except (UnicodeDecodeError, IOError):
        pass
    return args