homework_checker/core/tools.py
def __run_subprocess(
    command: Union[List[str], str],
    str_input: str = None,
    timeout: float = None,
    check: bool = False,
    **kwargs
) -> subprocess.CompletedProcess:
    """Run a command as a subprocess.

    Using the guide from StackOverflow:
    https://stackoverflow.com/a/36955420/1763680
    This command has been adapted from:
    https://github.com/python/cpython/blob/3.5/Lib/subprocess.py#L352-L399

    This code does essentially the same as subprocess.run(...) but makes sure to
    kill the whole process tree which allows to use the timeout even when using
    shell=True. The reason I don't want to stop using shell=True here is the
    convenience of piping arguments from one function to another.
    """
    if str_input is not None:
        if "stdin" in kwargs:
            raise ValueError("stdin and str_input arguments may not both be used.")
        kwargs["stdin"] = subprocess.PIPE

    if timeout is None:
        timeout = 20
    with Popen(command, start_new_session=True, **kwargs) as process:
        try:
            stdout, stderr = process.communicate(str_input, timeout=timeout)
        except TimeoutExpired as timeout_error:
            # Kill the whole group of processes.
            os.killpg(process.pid, signal.SIGINT)
            stdout, stderr = process.communicate()
            raise TimeoutExpired(
                process.args, timeout, output=stdout, stderr=stderr
            ) from timeout_error
        retcode = process.poll()
        if retcode is None:
            retcode = 1
        if check and retcode:
            raise CalledProcessError(
                retcode, process.args, output=stdout, stderr=stderr
            )
    return CompletedProcess(process.args, retcode, stdout, stderr)
Run a command as a subprocess.

    Using the guide from StackOverflow:
    https://stackoverflow.com/a/36955420/1763680
    This command has been adapted from:
    https://github.com/python/cpython/blob/3.5/Lib/subprocess.py#L352-L399

    This code does essentially the same as subprocess.run(...) but makes sure to
    kill the whole process tree which allows to use the timeout even when using
    shell=True. The reason I don't want to stop using shell=True here is the
    convenience of piping arguments from one function to another.
    
Run a command as a subprocess.

This code does essentially the same as subprocess.run(...) but makes sure to
kill the whole process tree which allows to use the timeout even when using
shell=True. The reason I don't want to stop using shell=True here is the
convenience of piping arguments from one function to another.
Run a command as a subprocess . This code does essentially the same as subprocess . run ( ... ) but makes sure to kill the whole process tree which allows to use the timeout even when using shell = True . The reason I don ' t want to stop using shell = True here is the convenience of piping arguments from one function to another .
def __run_subprocess(
    command: Union[List[str], str],
    str_input: str = None,
    timeout: float = None,
    check: bool = False,
    **kwargs
) -> subprocess.CompletedProcess:
    if str_input is not None:
        if "stdin" in kwargs:
            raise ValueError("stdin and str_input arguments may not both be used.")
        kwargs["stdin"] = subprocess.PIPE
    if timeout is None:
        timeout = 20
    with Popen(command, start_new_session=True, **kwargs) as process:
        try:
            stdout, stderr = process.communicate(str_input, timeout=timeout)
        except TimeoutExpired as timeout_error:
            os.killpg(process.pid, signal.SIGINT)
            stdout, stderr = process.communicate()
            raise TimeoutExpired(
                process.args, timeout, output=stdout, stderr=stderr
            ) from timeout_error
        retcode = process.poll()
        if retcode is None:
            retcode = 1
        if check and retcode:
            raise CalledProcessError(
                retcode, process.args, output=stdout, stderr=stderr
            )
    return CompletedProcess(process.args, retcode, stdout, stderr)
def __run_subprocess ( command : Union [ List [ str ] , str ] , str_input : str = None , timeout : float = None , check : bool = False , ** kwargs ) -> subprocess . CompletedProcess : if str_input is not None : if "stdin" in kwargs : raise ValueError ( "stdin and str_input arguments may not both be used." ) kwargs [ "stdin" ] = subprocess . PIPE if timeout is None : timeout = 20 with Popen ( command , start_new_session = True , ** kwargs ) as process : try : stdout , stderr = process . communicate ( str_input , timeout = timeout ) except TimeoutExpired as timeout_error : os . killpg ( process . pid , signal . SIGINT ) stdout , stderr = process . communicate ( ) raise TimeoutExpired ( process . args , timeout , output = stdout , stderr = stderr ) from timeout_error retcode = process . poll ( ) if retcode is None : retcode = 1 if check and retcode : raise CalledProcessError ( retcode , process . args , output = stdout , stderr = stderr ) return CompletedProcess ( process . args , retcode , stdout , stderr )
Run a command as a subprocess.
Run a command as a subprocess .
"""Run a command as a subprocess.

    Using the guide from StackOverflow:
    https://stackoverflow.com/a/36955420/1763680
    This command has been adapted from:
    https://github.com/python/cpython/blob/3.5/Lib/subprocess.py#L352-L399

    This code does essentially the same as subprocess.run(...) but makes sure to
    kill the whole process tree which allows to use the timeout even when using
    shell=True. The reason I don't want to stop using shell=True here is the
    convenience of piping arguments from one function to another.
    """ # Kill the whole group of processes.
command
Union[List[str], str]
str_input
str
timeout
float
check
bool