nat.tool.code_execution.local_sandbox.local_sandbox_server#

Attributes#

Classes#

CodeExecutionStatus

Status of code execution.

CodeExecutionResult

Result of code execution.

CodeExecutionResponse

Response class that returns a JSON response with the given status code and result.

Functions#

add_hsts_header(response)

execute_python(→ CodeExecutionResult)

Execute Python code in a subprocess.

execute_code_subprocess(generated_code, queue)

Execute code in a subprocess.

do_execute(→ CodeExecutionResponse)

Main function to handle execution requests.

execute()

Module Contents#

app#
logger#
class CodeExecutionStatus#

Bases: str, enum.Enum

Status of code execution.

Initialize self. See help(type(self)) for accurate signature.

COMPLETED = 'completed'#
ERROR = 'error'#
TIMEOUT = 'timeout'#
class CodeExecutionResult(/, **data: Any)#

Bases: pydantic.BaseModel

Result of code execution.

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

process_status: CodeExecutionStatus = None#
stdout: str = None#
stderr: str = None#
class CodeExecutionResponse(status_code: int, result: CodeExecutionResult)#

Bases: flask.Response

Response class that returns a JSON response with the given status code and result.

classmethod with_error(
status_code: int,
error_message: str,
) CodeExecutionResponse#
add_hsts_header(response)#
execute_python(
generated_code: str,
timeout: float,
) CodeExecutionResult#

Execute Python code in a subprocess.

Args:

generated_code: The code to execute timeout: The timeout for the execution

Returns:

CodeExecutionResult object containing the execution result

execute_code_subprocess(generated_code: str, queue)#

Execute code in a subprocess.

Args:

generated_code: The code to execute queue: The queue to put the result in

do_execute(request: flask.Request) CodeExecutionResponse#

Main function to handle execution requests.

Args:

request: Request object containing the execution request

Returns:

CodeExecutionResponse object containing the execution result

execute()#