Skip to main content

A lightweight Python library for executing JavaScript in a QuickJS sandbox

Project description

qixo

PyPI Tests License

A lightweight Python library for executing JavaScript in a QuickJS sandbox.

Why qixo?

  • Fast: QuickJS starts in milliseconds with minimal memory overhead
  • Safe: Execute untrusted JavaScript with memory and time limits
  • Simple: Clean Python API with automatic JSON data exchange
  • Lightweight: ~700KB QuickJS engine, no heavy dependencies

Installation

pip install qixo

Quick Start

from qixo import Qixo

# Basic usage
with Qixo() as box:
    result = box.eval("1 + 1")
    print(result)  # 2
    
    # JSON data exchange
    result = box.eval("({name: 'qixo', version: 1})")
    print(result)  # {'name': 'qixo', 'version': 1}
    
    # State persists between evals
    box.eval("var x = 10")
    box.eval("var y = 20")
    result = box.eval("x + y")
    print(result)  # 30

Features

Memory and Time Limits

# Limit memory to 10MB and execution time to 1 second
with Qixo(memory_limit_mb=10, timeout_ms=1000) as box:
    result = box.eval("'a'.repeat(1000)")  # Works fine
    
    try:
        # This will raise QixoError due to memory limit
        box.eval("'a'.repeat(100000000)")
    except QixoError as e:
        print(f"Error: {e}")

Error Handling

from qixo import Qixo, QixoError

with Qixo() as box:
    try:
        box.eval("throw new Error('Something went wrong')")
    except QixoError as e:
        print(f"JavaScript error: {e}")
        print(f"Stack trace: {e.stack}")

Working with Functions

with Qixo() as box:
    # Define a function
    box.eval("""
        function fibonacci(n) {
            if (n <= 1) return n;
            return fibonacci(n - 1) + fibonacci(n - 2);
        }
    """)
    
    # Call it
    result = box.eval("fibonacci(10)")
    print(result)  # 55

Development

# Clone the repository
git clone https://github.com/jeorgexyz/qixo.git
cd qixo

# Install development dependencies
pip install -e ".[dev]"

# Run tests
pytest

# Run tests with coverage
pytest --cov=qixo --cov-report=html

License

Apache 2.0

Credits

Built on top of the excellent quickjs Python bindings.

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

qixo-0.1.1.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

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

qixo-0.1.1-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file qixo-0.1.1.tar.gz.

File metadata

  • Download URL: qixo-0.1.1.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for qixo-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2039cd4b29875adf6799744fc8d0950b759ca28f2e1903d264d192822f6539ac
MD5 5d7a3e9271f702b2726b780bcbaee811
BLAKE2b-256 559bb7adf89012bff65b3b0236495482367e2766f916524868dc5c44dbabaa20

See more details on using hashes here.

File details

Details for the file qixo-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: qixo-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for qixo-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ab496b72f1cb3855a5267e066e28324f5f7ab16996d2cb3ab5801950b9489e0b
MD5 24a296b4461c125401c7f0bdba7a8acb
BLAKE2b-256 91ad8e6da9d2bef94de627e61f8bc061b4f5fb3fa290120b1bb28edadf2f64c0

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