Pydantic-Monty

0.0.12 · active · verified Thu Apr 16

Pydantic-Monty provides Python bindings for the Monty sandboxed Python interpreter, a Rust-based virtual machine designed for securely executing untrusted Python code. It is currently at version 0.0.12 and sees frequent, rapid releases as it is under active development, indicating ongoing feature additions and potential API changes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Monty VM, execute Python code within its sandboxed environment, and handle exceptions raised by the VM.

from monty import Monty, VmException

# Initialize the Monty VM
vm = Monty()

# Run some simple Python code inside the VM
result = vm.run('x = 10\ny = 20\nprint(x + y)')
print(f'stdout: {result.stdout.strip()!r}')
print(f'stderr: {result.stderr.strip()!r}')
print(f'return_value: {result.return_value!r}')

# Example of handling a VM-level exception
try:
    vm.run('raise ValueError("Something went wrong in the sandbox!")')
except VmException as e:
    print(f'VM error caught: {e}')

view raw JSON →