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
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 pyrex3
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]})
print(result)
📚 Advanced Usage
Custom Compiler Settings
from pyrex.languages.rust import RustCompiler
from pyrex.core.base import CompilerConfig
compiler = RustCompiler(
config=CompilerConfig(
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);
""")
print(result)
Error Handling
from pyrex.exceptions import PyrexCompileError
try:
rust.execute("let x = ;") # Invalid syntax
except PyrexCompileError as e:
print(f"Error:\n {e}")
🔒 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 runvariables(dict, optional) – Injected variablestimeout(float, default=30s) – Max runtimeforce_recompile(bool) – Ignore cache, force rebuildfast(bool) – Skips few checks to get faster compilation
Returns:
- Execution output as
str
Raises:
PyrexCompileError– Compilation failedPyrexRuntimeError– Runtime errorPyrexTypeError– Type conversion issuePyrexSecurityError– Security violation
🤝 Contributing
- Fork this repository
- Create a feature branch:
git checkout -b feature/my-feature - Make your changes
- 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyrex3-1.0.4.tar.gz.
File metadata
- Download URL: pyrex3-1.0.4.tar.gz
- Upload date:
- Size: 40.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
99389c038c10a37d35507991b97f298c77aabf75e863cdcaf901083704ffcc63
|
|
| MD5 |
3d697ae3559fa00750e4c6a79389ff91
|
|
| BLAKE2b-256 |
40f91cf22295621d347573ddf20921cfc7a38476365a7e084bbad75cc241b16f
|
File details
Details for the file pyrex3-1.0.4-py3-none-any.whl.
File metadata
- Download URL: pyrex3-1.0.4-py3-none-any.whl
- Upload date:
- Size: 45.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
87ec316b65612d375a7dd3702d247cbd96194bfaaf24701bffcfe35364756551
|
|
| MD5 |
9ad34ce700b9d973afc13150b997193d
|
|
| BLAKE2b-256 |
de1651c1d5c35586d1e81be4f29403813372bfa02f2537fd5ded0c1fe5513c8b
|