Skip to main content

Lightning-fast multi-language code execution engine with security-first design

Project description

🚀 Pyrex

Seamless inline Rust, C, and C++ execution inside Python — with enterprise-grade safety, performance, and simplicity

PyPI Python License

Write native Rust, C, and C++ code inline with Python. Automatically compiled, cached, and sandboxed — ready for production.


✨ Features

  • 🦀 Rust-first – Full Rust support with automatic compilation
  • C & C++ support – Modern C/C++ with type-safe bridging
  • 🔒 Enterprise security – Sandboxed execution, validation, and resource limits
  • 🚀 Smart caching – Compiles once, executes instantly on repeat
  • 🎯 Type-safe – Automatic Python ↔ native type conversion
  • 📊 Detailed errors – Rich compile/runtime diagnostics with context
  • 🔧 Zero config – Works out of the box, no setup needed

🛠️ Installation

pip install pyrex

Requirements:
You’ll need rustc, gcc/clang, and g++/clang++ installed.


🚀 Quick Start

Rust Example

from pyrex.languages.rust import rust

result = rust.execute("""
    let numbers = vec![1, 2, 3, 4, 5];
    let sum: i32 = numbers.iter().sum();
    println!("Sum: {}", sum);
""")
print(result)  # "Sum: 15"

C Example

from pyrex import c

result = c.execute("""
    int result = x * y + z;
    printf("Result: ```d\\n", result);
""", {"x": 10, "y": 20, "z": 5}, fast=True)

print(result)  # "Result: 205"

C++ Example

from pyrex import cpp

result = cpp.execute("""
    std::vector<int> data = numbers;
    std::sort(data.begin(), data.end());

    std::cout << "Sorted: ";
    for (int n : data) std::cout << n << " ";
    std::cout << std::endl;
""", variables={"numbers": [64, 34, 25, 12, 22, 11, 90]})

📚 Advanced Usage

Custom Compiler Settings

from pyrex import RustCompiler

compiler = RustCompiler(
    compile_flags=["-O", "--edition", "2021"],
    cache_dir="/tmp/pyrex_cache",
    enable_security=True
)

result = compiler.execute("""
    let result = (0..1_000_000).sum::<i64>();
    println!("Sum: {}", result);
""")

Error Handling

from pyrex.exceptions import PyrexCompileError, PyrexRuntimeError

try:
    rust.execute("let x = ;")  # Invalid syntax
except PyrexCompileError as e:
    print(f"Compilation failed at line {e.line_number}: {e.message}")
    print(f"Snippet:\n{e.code_snippet}")

🔒 Security

Pyrex enforces enterprise-grade safety out of the box:

  • Static analysis for dangerous patterns
  • Sandboxed execution in temp directories
  • Input sanitization & type validation
  • Memory, CPU, and timeout limits

🎯 Type Mapping

Python Type Rust C C++
bool bool bool bool
int i64 long long long long
float f64 double double
str String char* std::string
list[int] Vec<i64> int[] std::vector<int>

⚡ Performance

  • First run: Compiles & caches the binary
  • Next runs: Executes instantly (10–100× faster)
  • Smart invalidation: Cache refreshes automatically when code or variables change

📖 API Reference

execute(code, variables={"key": "value"}, timeout=30.0, force_recompile=False, fast=True)

Parameters:

  • code (str) – Source code to compile and run
  • variables (dict, optional) – Injected variables
  • timeout (float, default=30s) – Max runtime
  • force_recompile (bool) – Ignore cache, force rebuild
  • fast (bool) – Skips few checks to get faster compilation

Returns:

  • Execution output as str

Raises:

  • PyrexCompileError – Compilation failed
  • PyrexRuntimeError – Runtime error
  • PyrexTypeError – Type conversion issue
  • PyrexSecurityError – Security violation

🤝 Contributing

  1. Fork this repository
  2. Create a feature branch: git checkout -b feature/my-feature
  3. Make your changes
  4. Submit a pull request 🎉

📄 License

Licensed under the MIT License. See the LICENSE file.


🙏 Acknowledgments

  • Built with ❤️ by Luciano Correia
  • Inspired by the need for frictionless multi-language execution
  • Thanks to all contributors and early testers

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

pyrex3-1.0.2.tar.gz (40.3 kB view details)

Uploaded Source

Built Distribution

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

pyrex3-1.0.2-py3-none-any.whl (45.6 kB view details)

Uploaded Python 3

File details

Details for the file pyrex3-1.0.2.tar.gz.

File metadata

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

File hashes

Hashes for pyrex3-1.0.2.tar.gz
Algorithm Hash digest
SHA256 29d950e8549de97091c263a5f866fb6d27ca98895f982d4e44a59a44e974f634
MD5 f31e259bc5a67e919f645862de68fb75
BLAKE2b-256 7052080ff32ea2b4bfa72b491d4bda64595a2c30391f298306985a2f5c737bb2

See more details on using hashes here.

File details

Details for the file pyrex3-1.0.2-py3-none-any.whl.

File metadata

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

File hashes

Hashes for pyrex3-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8a3c82d09198efa51997134f77e21c865132630efc1f5fbcd966d0e6f78f9402
MD5 86048d0fe223134349a43a9b0341faff
BLAKE2b-256 77875cef9ae8c225a6511445b7ed8a1e1ffd36e6658469ae1964799389bced56

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