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.0.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.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: qixo-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 80a2d722d8c36ca75440eb1b9a497dd64546c9a207d4eb54bb85cffea2460341
MD5 67ba6eac21a2d2e5bbd01672153b1965
BLAKE2b-256 0af17d1c7987e049648bd2cb72fa09b2d318131e2c1a5d1166c48dfc2fadc8ae

See more details on using hashes here.

File details

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

File metadata

  • Download URL: qixo-0.1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ef3cc9a75f59bf9a1398167e35702ffd255b9c525e8d2c0465f0105cfcfdf7be
MD5 11e099a83b359080aef21f18174c754b
BLAKE2b-256 aeb1234d95ff24aa37d07015b9bb87bb31bdfb01213f31a8632989aacdfb6932

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