Execute code snippets in isolated Docker environments with multi-language support and security protection
Project description
coex - Code Execution Library
coex is a Python library that executes code snippets using subprocess calls on the host system. It provides secure, multi-language code execution with comprehensive validation and testing capabilities.
Features
- Host System Execution: Uses subprocess to run language tools directly on the host
- Security First: Protection against destructive file operations and malicious code
- Multi-Language Support: Python, JavaScript, Java, C++, C, Go, Rust, and more
- Multiple Execution Modes: Input/output validation, function comparison, simple execution
- High Performance: Direct subprocess execution without container overhead
- Robust Error Handling: Comprehensive error handling and timeout management
- Comprehensive Testing: Extensive test suite with security and integration tests
Quick Start
Installation
pip install coex
Prerequisites
- Python 3.8 or higher
- Language tools installed on host system:
- Java:
javacandjava(OpenJDK 8+ or Oracle JDK) - JavaScript:
node(Node.js 14+) - C/C++:
gccandg++(GCC 7+) - Go:
go(Go 1.16+) - Rust:
rustc(Rust 1.50+)
- Java:
- Docker (optional, for sandboxed execution)
Basic Usage
import coex
# Method 1: Input/Output validation (explicit mode)
result = coex.execute(
mode="answer",
inputs=[0, 1, 2, 3],
outputs=[1, 2, 3, 4],
code="def add_one(x): return x + 1",
language="python"
)
# Returns: [1, 1, 1, 1] (boolean array indicating pass/fail for each test case)
# Method 2: Function comparison (explicit mode)
result = coex.execute(
mode="function",
answer_fn="def say_hello(): return 'hello world'",
code="def hello(): return 'hello world'",
language="python"
)
# Returns: [1] (boolean indicating if functions return same value)
# Method 3: Backward compatibility (auto-detection)
result = coex.execute(
inputs=[1, 2, 3],
outputs=[2, 4, 6],
code="def double(x): return x * 2",
language="python"
)
# Returns: [1, 1, 1] (auto-detects "answer" mode)
# Method 4: Simple execution
result = coex.execute(code="print('Hello, World!')", language="python")
# Returns: [1] (boolean indicating successful execution)
# Docker cleanup
coex.rm_docker() # Remove cached Docker containers
Supported Languages
| Language | File Extension | Docker Image | Aliases |
|---|---|---|---|
| Python | .py |
python:3.11-slim |
py |
| JavaScript | .js |
node:18-slim |
js |
| Java | .java |
openjdk:11-slim |
|
| C++ | .cpp |
gcc:latest |
c++, cxx |
| C | .c |
gcc:latest |
|
| Go | .go |
golang:1.19-slim |
|
| Rust | .rs |
rust:slim |
rs |
API Reference
coex.execute()
Execute code snippets in isolated Docker environments.
Parameters:
inputs(List[Any], optional): List of input values for testingoutputs(List[Any], optional): List of expected output valuescode(str, optional): Code to executeanswer_fn(str, optional): Reference function code for comparisonlanguage(str, default="python"): Programming languagetimeout(int, optional): Execution timeout in secondsmode(str, optional): Execution mode ("answer" for input/output validation, "function" for function comparison, None for auto-detection)
Returns:
List[int]: List of integers (0 or 1) indicating pass/fail for each test case
Raises:
SecurityError: If dangerous code is detectedValidationError: If input validation failsExecutionError: If code execution failsDockerError: If Docker operations fail
New in v0.1.0:
- Explicit Mode Parameter: Use
mode="answer"ormode="function"for explicit execution modes - 3-Second Timeout: Each test case has a 3-second timeout limit
- Unique File Naming: Automatic unique file naming for batch processing support
coex.rm_docker()
Remove all cached Docker containers.
Parameters: None
Returns: None
Execution Modes
1. Input/Output Validation Mode
Test code against specific input/output pairs:
inputs = [1, 2, 3, 4, 5]
outputs = [1, 4, 9, 16, 25]
code = """
def square(x):
return x * x
"""
result = coex.execute(inputs=inputs, outputs=outputs, code=code)
# Tests: square(1)==1, square(2)==4, square(3)==9, etc.
2. Function Comparison Mode
Compare two functions to see if they produce the same output:
answer_fn = """
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n-1) + fibonacci(n-2)
"""
code = """
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return a
"""
result = coex.execute(answer_fn=answer_fn, code=code)
# Compares if both functions return the same value
3. Simple Execution Mode
Execute code and check if it runs without errors:
code = """
import math
print(f"Pi is approximately {math.pi:.2f}")
"""
result = coex.execute(code=code, language="python")
# Returns [1] if execution succeeds, [0] if it fails
Security Features
coex includes comprehensive security protection:
Blocked Operations
- File system operations (
rm,mkdir,chmod, etc.) - Network operations (
wget,curl,ssh, etc.) - System operations (
sudo,su, etc.) - Dangerous imports (
os,subprocess,sys, etc.) - Code evaluation (
eval,exec,__import__, etc.)
Example Security Protection
dangerous_code = """
import os
os.system("rm -rf /") # This will be blocked!
"""
result = coex.execute(code=dangerous_code)
# Returns [0] - execution blocked for security
Multi-Language Examples
Python
code = """
def greet(name):
return f"Hello, {name}!"
"""
result = coex.execute(code=code, language="python")
JavaScript
code = """
function greet(name) {
return `Hello, ${name}!`;
}
"""
result = coex.execute(code=code, language="javascript")
Java
code = """
public class Greeter {
public String greet(String name) {
return "Hello, " + name + "!";
}
}
"""
result = coex.execute(code=code, language="java")
C++
code = """
#include <iostream>
#include <string>
std::string greet(std::string name) {
return "Hello, " + name + "!";
}
"""
result = coex.execute(code=code, language="cpp")
Configuration
Environment Variables
COEX_DOCKER_TIMEOUT: Docker operation timeout (default: 300 seconds)COEX_DOCKER_MEMORY_LIMIT: Container memory limit (default: "512m")COEX_EXECUTION_TIMEOUT: Code execution timeout (default: 30 seconds)COEX_TEMP_DIR: Temporary directory for files (default: "/tmp/coex")COEX_DISABLE_SECURITY: Disable security checks (default: False)
Custom Configuration
from coex.config.settings import settings
# Modify settings
settings.set("execution.timeout", 60)
settings.set("docker.memory_limit", "1g")
settings.set("security.enable_security_checks", False)
Error Handling
coex provides comprehensive error handling:
try:
result = coex.execute(code="invalid syntax", language="python")
except coex.SecurityError as e:
print(f"Security violation: {e}")
except coex.ValidationError as e:
print(f"Validation error: {e}")
except coex.ExecutionError as e:
print(f"Execution failed: {e}")
except coex.DockerError as e:
print(f"Docker error: {e}")
Performance Tips
- Container Reuse: Containers are cached automatically for better performance
- Batch Operations: Process multiple test cases in single calls when possible
- Timeout Management: Set appropriate timeouts for your use case
- Memory Limits: Adjust memory limits based on your code requirements
- Cleanup: Use
coex.rm_docker()to clean up containers when done
Development
Running Tests
# Install development dependencies
pip install -e ".[dev]"
# Run all tests
pytest
# Run specific test categories
pytest tests/test_security.py -v
pytest tests/test_integration.py -v -m integration
# Run with coverage
pytest --cov=coex --cov-report=html
Code Quality
# Format code
black coex/ tests/
# Lint code
flake8 coex/ tests/
# Type checking
mypy coex/
Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Make your changes
- Add tests for your changes
- Ensure all tests pass (
pytest) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Changelog
v0.1.0 (Initial Release)
- Docker-based code execution
- Multi-language support (Python, JavaScript, Java, C++, C, Go, Rust)
- Security protection against dangerous operations
- Input/output validation mode
- Function comparison mode
- Simple execution mode
- Comprehensive test suite
- Container caching for performance
- Robust error handling
Support
Acknowledgments
- Docker for containerization technology
- The Python community for excellent tooling and libraries
- Contributors and testers who helped make this project possible
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 coex-0.1.19.tar.gz.
File metadata
- Download URL: coex-0.1.19.tar.gz
- Upload date:
- Size: 60.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
af6b70c2b6c77f45d98e9bb33353a213c2c4d81d5a99bc024c3003246b66b7ce
|
|
| MD5 |
bbff8080f25f186d8f5c99e4dc32061f
|
|
| BLAKE2b-256 |
c9dac0ca55b94bc9e0715294a1bb5b07e0733f2a2f2294e866553d120031f812
|
File details
Details for the file coex-0.1.19-py3-none-any.whl.
File metadata
- Download URL: coex-0.1.19-py3-none-any.whl
- Upload date:
- Size: 40.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bd61c4401bfa8fecc04507b5aea53416307f4c2f6a5302daa5428ab4dbf95cf0
|
|
| MD5 |
c0b1402e08c3f4ba96fb7c8b085099ac
|
|
| BLAKE2b-256 |
dee7dc05adb3c6f965347f54fa6b7d151df44d489690bc9b6407716478ce9db3
|