Overview
TheMonty class is the main entry point for executing sandboxed Python code in JavaScript/TypeScript. It parses Python code and provides methods to execute it with configurable inputs, external functions, and resource limits.
Constructor
string
required
The Python code to parse and prepare for execution
MontyOptions
Optional configuration for the interpreter
MontySyntaxError- If the code has syntax errorsMontyTypingError- If type checking is enabled and finds errors
Example
Methods
run()
RunOptions
Optional execution configuration
any
The result of the last expression in the Python code
MontyRuntimeError- If the code raises an exception during execution
start()
StartOptions
Same as
RunOptions - optional execution configurationMontySnapshot | MontyNameLookup | MontyComplete
MontySnapshot- Execution paused at an external function callMontyNameLookup- Execution paused at an undefined name lookupMontyComplete- Execution completed successfully
MontyRuntimeError- If the code raises an exception
typeCheck()
string
Optional code to prepend before type checking (e.g., type definitions)
MontyTypingError- If type checking finds errors
dump()
Buffer
Binary representation of the Monty instance
load() (static)
Buffer
required
Binary data from
dump()Monty
Restored Monty instance ready to execute
Properties
scriptName
inputs
Helper Function: runMontyAsync()
Monty
required
The Monty instance to execute
RunMontyAsyncOptions
any
The output of the Monty script
MontyRepl
TheMontyRepl class provides an incremental, no-replay REPL (Read-Eval-Print Loop) session. Each feed() call compiles and executes only the provided snippet against preserved heap/global state.
create() (static method)
string
required
Initial Python code to execute
MontyOptions
Configuration options (scriptName, inputs, typeCheck, etc.)
StartOptions
Execution options for the initial code (inputs, limits)
MontyRepl
A new REPL session instance
MontySyntaxError- If the code has syntax errorsMontyRuntimeError- If execution failsMontyTypingError- If type checking is enabled and finds errors
scriptName (property)
feed()
string
required
Python code snippet to execute
any
The result of the code snippet
MontyRuntimeError- If execution fails
dump()
Buffer
Serialized REPL session data
load() (static method)
Buffer
required
Serialized REPL data from
dump()MontyRepl
Restored REPL session
ValueError- If deserialization fails
