SubprocessInterpreter

class SubprocessInterpreter(BaseInterpreter):

SubprocessInterpreter is a class for executing code files or code strings in a subprocess.

This class handles the execution of code in different scripting languages (currently Python and Bash) within a subprocess, capturing their stdout and stderr streams, and allowing user checking before executing code strings.

Parameters:

  • require_confirm (bool, optional): If True, prompt user before running code strings for security. (default: :obj:True)
  • print_stdout (bool, optional): If True, print the standard output of the executed code. (default: :obj:False)
  • print_stderr (bool, optional): If True, print the standard error of the executed code. (default: :obj:True)
  • execution_timeout (int, optional): Maximum time in seconds to wait for code execution to complete. (default: :obj:60)

init

def __init__(
    self,
    require_confirm: bool = True,
    print_stdout: bool = False,
    print_stderr: bool = True,
    execution_timeout: int = 60
):

run_file

def run_file(self, file: Path, code_type: str):

Executes a code file in a subprocess and captures its output.

Parameters:

  • file (Path): The path object of the file to run.
  • code_type (str): The type of code to execute (e.g., ‘python’, ‘bash’).

Returns:

str: A string containing the captured stdout and stderr of the executed code.

run

def run(self, code: str, code_type: str):

Generates a temporary file with the given code, executes it, and deletes the file afterward.

Parameters:

  • code (str): The code string to execute.
  • code_type (str): The type of code to execute (e.g., ‘python’, ‘bash’).

Returns:

str: A string containing the captured stdout and stderr of the executed code.

_create_temp_file

def _create_temp_file(self, code: str, extension: str):

Creates a temporary file with the given code and extension.

Parameters:

  • code (str): The code to write to the temporary file.
  • extension (str): The file extension to use.

Returns:

Path: The path to the created temporary file.

_check_code_type

def _check_code_type(self, code_type: str):

supported_code_types

def supported_code_types(self):

Provides supported code types by the interpreter.

update_action_space

def update_action_space(self, action_space: Dict[str, Any]):

Updates action space for python interpreter

_is_command_available

def _is_command_available(self, command: str):

Check if a command is available in the system PATH.

Parameters:

  • command (str): The command to check.

Returns:

bool: True if the command is available, False otherwise.