Skip to content

Conversation

@danbugs
Copy link
Contributor

@danbugs danbugs commented Dec 19, 2025

Adds Python bindings for hyperlight-nanvix using PyO3. Python users can now run JavaScript, Python, C, and C++ workloads in sandboxes with the same API as the Node.js bindings.

Setup:

pipx install maturin
python3 -m venv .venv && source .venv/bin/activate
maturin develop --features python
python 

Usage:

import asyncio
from hyperlight_nanvix import NanvixSandbox

async def main():
    sandbox = NanvixSandbox()
    result = await sandbox.run('guest-examples/hello.js')
    if result.success:
        print('Execution completed')

asyncio.run(main())

Changes include PyO3 bindings with async support, maturin build configuration, type stubs, and an example that follows the same pattern as the Node.js SDK.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds Python bindings for hyperlight-nanvix using PyO3, enabling Python developers to run JavaScript, Python, C, and C++ workloads in sandboxed environments with an API that mirrors the existing Node.js SDK.

Key Changes:

  • Implements PyO3-based Python bindings with async support via pyo3-asyncio and Tokio runtime
  • Adds type stubs for IDE support and type checking
  • Provides complete Python package configuration with maturin build system

Reviewed changes

Copilot reviewed 8 out of 10 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/python.rs Core PyO3 implementation providing NanvixSandbox, SandboxConfig, and WorkloadResult classes with async methods
src/lib.rs Adds conditional compilation flag for python feature module
python/hyperlight_nanvix/init.pyi Type stub definitions for IDE autocomplete and type checking
python/hyperlight_nanvix/init.py Python package initialization with version and exports
pyproject.toml Maturin build configuration for Python packaging
Cargo.toml Adds optional pyo3 and pyo3-asyncio dependencies with python feature flag
Cargo.lock Lock file updates with new PyO3 dependency tree
examples/python_sdk_example.py Example demonstrating Python SDK usage with async/await
README.md Documentation for Python setup, usage examples, and API reference
.gitignore Adds Python-specific ignore patterns for virtual environments and build artifacts

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 128 to 136
fn clear_cache<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
let runtime = Arc::clone(&self.runtime);

pyo3_asyncio::tokio::future_into_py(py, async move {
runtime.clear_cache().await
.map_err(|e| PyRuntimeError::new_err(format!("Failed to clear cache: {}", e)))
})
}
Copy link

Copilot AI Dec 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Python binding's clear_cache method has an inconsistent return type compared to the Node.js binding. The Node.js version returns a boolean (true on success, error on failure), while the Python version returns None on success. For API consistency across language bindings, consider returning a boolean value in the Python binding as well. This would make the Python API more predictable and align with the Node.js SDK pattern.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in e0e69cf.

- Add pyo3 and pyo3-asyncio dependencies with python feature flag
- Implement Python bindings in src/python.rs mirroring napi.rs structure
- Add pyproject.toml for maturin build configuration
- Create Python package structure with type stubs
- Support async/await for sandbox operations

Signed-off-by: danbugs <danilochiarlone@gmail.com>
- Add python_sdk_example.py following napi.js pattern
- Update README with Python setup and usage instructions
- Include one-time maturin installation steps

Signed-off-by: danbugs <danilochiarlone@gmail.com>
- Ignore .venv/, __pycache__/, and Python compiled files
- Ignore compiled .so files in python/ directory

Signed-off-by: danbugs <danilochiarlone@gmail.com>
- Changes clear_cache to return bool (true on success)
- Updates type stub to reflect bool return type
- Aligns Python SDK with Node.js SDK behavior

Addresses PR #20 feedback from @copilot-pull-request-reviewer
Related issues: #22 (tests), #23 (code strings)

Signed-off-by: danbugs <danilochiarlone@gmail.com>
@danbugs danbugs force-pushed the danbugs/python-host-sdk branch from 374d0cb to e0e69cf Compare December 19, 2025 20:22
@danbugs danbugs merged commit fad4fd6 into main Dec 19, 2025
6 checks passed
@danbugs danbugs deleted the danbugs/python-host-sdk branch December 19, 2025 20:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants