(
self,
sandbox: Literal[
"internal_python", "jupyter", "docker", "subprocess", "e2b"
] = "internal_python",
verbose: bool = False,
unsafe_mode: bool = False,
import_white_list: Optional[List[str]] = None,
require_confirm: bool = False,
)
| 40 | """ |
| 41 | |
| 42 | def __init__( |
| 43 | self, |
| 44 | sandbox: Literal[ |
| 45 | "internal_python", "jupyter", "docker", "subprocess", "e2b" |
| 46 | ] = "internal_python", |
| 47 | verbose: bool = False, |
| 48 | unsafe_mode: bool = False, |
| 49 | import_white_list: Optional[List[str]] = None, |
| 50 | require_confirm: bool = False, |
| 51 | ) -> None: |
| 52 | self.verbose = verbose |
| 53 | self.unsafe_mode = unsafe_mode |
| 54 | self.import_white_list = import_white_list or list() |
| 55 | |
| 56 | # Type annotation for interpreter to allow all possible types |
| 57 | self.interpreter: Union[ |
| 58 | InternalPythonInterpreter, |
| 59 | JupyterKernelInterpreter, |
| 60 | DockerInterpreter, |
| 61 | SubprocessInterpreter, |
| 62 | E2BInterpreter, |
| 63 | ] |
| 64 | |
| 65 | if sandbox == "internal_python": |
| 66 | self.interpreter = InternalPythonInterpreter( |
| 67 | unsafe_mode=self.unsafe_mode, |
| 68 | import_white_list=self.import_white_list, |
| 69 | ) |
| 70 | elif sandbox == "jupyter": |
| 71 | self.interpreter = JupyterKernelInterpreter( |
| 72 | require_confirm=require_confirm, |
| 73 | print_stdout=self.verbose, |
| 74 | print_stderr=self.verbose, |
| 75 | ) |
| 76 | elif sandbox == "docker": |
| 77 | self.interpreter = DockerInterpreter( |
| 78 | require_confirm=require_confirm, |
| 79 | print_stdout=self.verbose, |
| 80 | print_stderr=self.verbose, |
| 81 | ) |
| 82 | elif sandbox == "subprocess": |
| 83 | self.interpreter = SubprocessInterpreter( |
| 84 | require_confirm=require_confirm, |
| 85 | print_stdout=self.verbose, |
| 86 | print_stderr=self.verbose, |
| 87 | ) |
| 88 | elif sandbox == "e2b": |
| 89 | self.interpreter = E2BInterpreter(require_confirm=require_confirm) |
| 90 | else: |
| 91 | raise RuntimeError( |
| 92 | f"The sandbox type `{sandbox}` is not supported." |
| 93 | ) |
| 94 | |
| 95 | def execute_code(self, code: str) -> str: |
| 96 | r"""Execute a given code snippet. |
nothing calls this directly
no test coverage detected