Skip to main content

Python sandbox powered by WebAssembly

Project description

PyEryx - Python Bindings for Eryx

Python bindings for the Eryx sandbox - execute Python code securely inside WebAssembly.

Installation

pip install pyeryx

Note: The package is installed as pyeryx but imported as eryx.

Or build from source using maturin:

cd crates/eryx-python
maturin develop

Quick Start

import eryx

# Create a sandbox with the embedded Python runtime
sandbox = eryx.Sandbox()

# Execute Python code in complete isolation
result = sandbox.execute('''
print("Hello from the sandbox!")
x = 2 + 2
print(f"2 + 2 = {x}")
''')

print(result.stdout)
# Output:
# Hello from the sandbox!
# 2 + 2 = 4

print(f"Execution took {result.duration_ms:.2f}ms")

Features

  • Complete Isolation: Sandboxed code cannot access files, network, or system resources
  • Resource Limits: Configure timeouts and memory limits
  • Fast Startup: Embedded pre-compiled runtime for minimal initialization overhead
  • Package Support: Load Python packages (.whl, .tar.gz) including native extensions
  • Type Safe: Full type stubs for IDE support and static analysis

API Reference

Sandbox

The main class for executing Python code in isolation.

sandbox = eryx.Sandbox(
    resource_limits=eryx.ResourceLimits(
        execution_timeout_ms=5000,      # 5 second timeout
        max_memory_bytes=100_000_000,   # 100MB memory limit
    )
)

result = sandbox.execute("print('Hello!')")

Loading Packages

You can load Python packages (wheels or tar.gz archives) into the sandbox:

import eryx

# Load packages from wheel files
sandbox = eryx.Sandbox(
    packages=[
        "/path/to/jinja2-3.1.2-py3-none-any.whl",
        "/path/to/markupsafe-2.1.3-wasi.tar.gz",  # WASI-compiled native extension
    ]
)

result = sandbox.execute('''
from jinja2 import Template
template = Template("Hello, {{ name }}!")
print(template.render(name="World"))
''')

For packages with native extensions (like markupsafe), you need WASI-compiled versions. These are automatically late-linked into the WebAssembly component.

You can also mount a pre-extracted site-packages directory:

sandbox = eryx.Sandbox(site_packages="/path/to/site-packages")

ExecuteResult

Returned by sandbox.execute() with execution results:

  • stdout: str - Captured standard output
  • duration_ms: float - Execution time in milliseconds
  • callback_invocations: int - Number of callback invocations
  • peak_memory_bytes: Optional[int] - Peak memory usage (if available)

ResourceLimits

Configure execution constraints:

limits = eryx.ResourceLimits(
    execution_timeout_ms=30000,        # Max script runtime (default: 30s)
    callback_timeout_ms=10000,         # Max single callback time (default: 10s)
    max_memory_bytes=134217728,        # Max memory (default: 128MB)
    max_callback_invocations=1000,     # Max callbacks (default: 1000)
)

# Or create unlimited (use with caution!)
unlimited = eryx.ResourceLimits.unlimited()

Exceptions

  • eryx.EryxError - Base exception for all Eryx errors
  • eryx.ExecutionError - Python code raised an exception
  • eryx.InitializationError - Sandbox failed to initialize
  • eryx.ResourceLimitError - Resource limit exceeded
  • eryx.TimeoutError - Execution timed out

Package Loading

Supported Formats

  • .whl - Standard Python wheels (zip archives)
  • .tar.gz / .tgz - Tarballs (used by wasi-wheels project)
  • Directories - Pre-extracted package directories

Native Extensions

Packages containing native Python extensions (.so files compiled for WASI) are automatically detected and late-linked into the WebAssembly component. This allows packages like numpy, markupsafe, and others to work in the sandbox.

Note: You need WASI-compiled versions of native extensions, not regular Linux/macOS/Windows binaries.

Error Handling

import eryx

sandbox = eryx.Sandbox()

try:
    result = sandbox.execute("raise ValueError('oops')")
except eryx.ExecutionError as e:
    print(f"Code failed: {e}")

try:
    sandbox = eryx.Sandbox(
        resource_limits=eryx.ResourceLimits(execution_timeout_ms=100)
    )
    result = sandbox.execute("while True: pass")
except eryx.TimeoutError as e:
    print(f"Timed out: {e}")

Development

Building

# Install maturin
pip install maturin

# Build and install in development mode
maturin develop

# Build release wheel
maturin build --release

Testing

pip install pytest
pytest

License

MIT OR Apache-2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyeryx-0.1.0.tar.gz (12.1 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

pyeryx-0.1.0-cp312-abi3-win_amd64.whl (34.6 MB view details)

Uploaded CPython 3.12+Windows x86-64

pyeryx-0.1.0-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (35.8 MB view details)

Uploaded CPython 3.12+manylinux: glibc 2.17+ x86-64

pyeryx-0.1.0-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (35.3 MB view details)

Uploaded CPython 3.12+manylinux: glibc 2.17+ ARM64

pyeryx-0.1.0-cp312-abi3-macosx_11_0_arm64.whl (35.0 MB view details)

Uploaded CPython 3.12+macOS 11.0+ ARM64

pyeryx-0.1.0-cp312-abi3-macosx_10_12_x86_64.whl (35.2 MB view details)

Uploaded CPython 3.12+macOS 10.12+ x86-64

File details

Details for the file pyeryx-0.1.0.tar.gz.

File metadata

  • Download URL: pyeryx-0.1.0.tar.gz
  • Upload date:
  • Size: 12.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.11.5

File hashes

Hashes for pyeryx-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9412f5c85f9df6f4da632eb7ea4a78744804adfbf14ba446c18301510568da81
MD5 f5dfcf89078405c8b78aaed922c9719c
BLAKE2b-256 c7b478abdeedaed022e7cd09825a06b3784bf8870d5192d8bd6d7fd932290828

See more details on using hashes here.

File details

Details for the file pyeryx-0.1.0-cp312-abi3-win_amd64.whl.

File metadata

  • Download URL: pyeryx-0.1.0-cp312-abi3-win_amd64.whl
  • Upload date:
  • Size: 34.6 MB
  • Tags: CPython 3.12+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.11.5

File hashes

Hashes for pyeryx-0.1.0-cp312-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 22964c1ab92bba882168c99a07bb6f772be51e895e536515ab6a0579f8d940d5
MD5 34f46723907a5c7291a477db2184105d
BLAKE2b-256 63eb6c4f7a41ab493445a896a842ec713015d58bcc4cf2bf5d947c61b9c1fe5c

See more details on using hashes here.

File details

Details for the file pyeryx-0.1.0-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyeryx-0.1.0-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f7c016ce56e22568dbc1f823b6542a726937b1af1f7ffca25cc9da91995e3721
MD5 8e745d7b209e3905452214505e7a80b3
BLAKE2b-256 dd96254a390097ba9770ef1718c370903bb72380520e669ec786b676f3a6002c

See more details on using hashes here.

File details

Details for the file pyeryx-0.1.0-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for pyeryx-0.1.0-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 08d86621143324b3d4498544d76f2aed0d582d2c186e45d53aa2a81aa0bc18c1
MD5 10c62d810af62afc7ef0edac48d444ca
BLAKE2b-256 3bf05e48782f2e3f7bb222deab1ec74ce43128a5d7575c1be67627343773c3de

See more details on using hashes here.

File details

Details for the file pyeryx-0.1.0-cp312-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyeryx-0.1.0-cp312-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5d0cb92d28b172b7fd71e9e93ac4bcab7370b5a136b7273cc3bf49780bcfa792
MD5 c4d31b1b554b5206b4b5089218985607
BLAKE2b-256 ccb92d27b5c953eff120dd623242f77d23efe73be00bc2aa1c72c01535b10609

See more details on using hashes here.

File details

Details for the file pyeryx-0.1.0-cp312-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for pyeryx-0.1.0-cp312-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 8eef72286869576d524e9fc3c482ccf679eeb864b55bba49fc6c820498567b5c
MD5 0e0a2636cb733e72a9bc53f29fbbdbfa
BLAKE2b-256 704d7847673a2fb3ab25bbc4a6c6cc947863f31c9716609c784bc26c50b54a2e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page