Skip to main content

Light-weight sandboxed environment powered by Linux containers

Project description

go-judge-py

A Pythonic wrapper around go-judge that provides self-managing, lightweight, rootless containers for secure code execution on Linux.

Unlike standard API clients, go-judge-py handles the entire lifecycle: it automatically downloads the sandbox binary, builds a root filesystem (rootfs) from official Linux distribution images (Debian, Alpine, Arch, Fedora), and manages the sandbox server for you.

Ideal for:

  • 🛡️ Verifying AI-generated code (LLM code interpreters)
  • ⚖️ Online Judge systems
  • 🏫 Computer Science Education (grading scripts)

✨ Features

  • Zero-Dependency Setup: Automatically fetches the go-judge binary and builds the execution environment.
  • Multi-Distro Support: Create environments based on Debian, Alpine, Arch Linux, or Fedora.
  • Rootless & Secure: Runs entirely in user namespaces (no sudo required) using unshare.
  • Parallel Execution: Native support for batch processing with concurrent requests.
  • Resource Control: Fine-grained limits on CPU, Memory, and Process count.

📦 Installation

pip install go-judge
# or with uv
uv add go-judge

Requirements: Linux system with unshare enabled (modern Kernels default).

1. Basic Usage (Python & C++)

The library comes with default configurations for common languages.

from go_judge import Sandbox

# Automatically builds a Debian environment with GCC and Python installed
# This might take a minute the first time to download the rootfs
with Sandbox("standard-env", distribution="debian", packages=["g++", "python3"]) as sb:
    # Run Python
    res = sb.run("python", "print('Hello from the Sandbox!')")
    print(res["stdout"])  # Output: Hello from the Sandbox!

    # Run C++ (Compiles and Executes)
    cpp_code = """
    #include <iostream>
    int main() { std::cout << "Fast C++" << std::endl; }
    """
    res = sb.run("cpp", cpp_code)
    print(res["stdout"])  # Output: Fast C++

2. Custom Languages & Resource Limits

You can register custom languages (like C) and set strict resource limits.

from go_judge import Sandbox

with Sandbox("custom-env", distribution="debian", packages=["gcc"]) as sb:
    # Register C configuration
    sb.register_language("c", {
        "src_name": "main.c",
        "bin_name": "main",
        "compile": {
            "args": ["/usr/bin/gcc", "main.c", "-o", "main"],
            "env": ["PATH=/usr/bin:/bin"]
        },
        "run": {
            "args": ["./main"],
            "env": ["PATH=/usr/bin:/bin"]
        }
    })

    # Run with constraints: 100ms CPU time, 32MB Memory
    result = sb.run(
        "c", 
        "int main() { return 0; }",
        exec_cpu_limit_ns=100_000_000,
        exec_memory_limit_b=33_554_432
    )

3. High-Performance Batch Processing

Execute code against multiple inputs in parallel using the built-in thread pool.

inputs = ["1 1", "2 2", "10 20"] # stdin for each test case
code = """
a, b = map(int, input().split())
print(a + b)
"""

with Sandbox("py-worker") as sb:
    # Returns a list of results, preserving order
    results = sb.run_multiple("python", code, inputs)
    
    for res in results:
        print(f"Status: {res['status']}, Output: {res['stdout'].strip()}")

🛠️ Supported Distributions

go-judge-py pulls metadata from the Linux Containers (LXC) project to build environments.

Distro Keyword Best For
Alpine alpine Ultra-lightweight, fast startup.
Debian debian Compatibility, standard glibc.
Fedora fedora Bleeding edge packages.
Arch arch Rolling release updates.

Example:

# Use a specific version of Debian
sb = Sandbox("old-stable", distribution="debian", release="bullseye")

💻 Development

Clone the repository and set up the environment using uv.

# Run integration tests (requires Linux)
uv run pytest -s tests/test_integration.py

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

go_judge-0.1.0.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

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

go_judge-0.1.0-py3-none-any.whl (13.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: go_judge-0.1.0.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for go_judge-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d9dbf274bb515fa6ec5e516a5bc13ec6440e5cf2d3db692fb8f33361f76be26a
MD5 46e04504d0924be2869d9dbfea39aec9
BLAKE2b-256 4f1651ff532b01c51a5ac58ade6861d7ae7e8097ea01286f0407f29549a704fa

See more details on using hashes here.

Provenance

The following attestation bundles were made for go_judge-0.1.0.tar.gz:

Publisher: publish.yml on criyle/go-judge-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: go_judge-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for go_judge-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4ac9ffa049ccfe9001d44e3b50d7b16974782419f29535aa552c85f4977c4fd6
MD5 0bb66578b02603b4475664747065a92c
BLAKE2b-256 61950f0af8a15ad06406a1277a6813bf1339d31106dd18e538292a7e7e9e7d4c

See more details on using hashes here.

Provenance

The following attestation bundles were made for go_judge-0.1.0-py3-none-any.whl:

Publisher: publish.yml on criyle/go-judge-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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