-
-
Notifications
You must be signed in to change notification settings - Fork 0
add: PyO3/Rust extension support investigation #116
Copy link
Copy link
Open
Labels
aigenAI-generated contentAI-generated content
Description
Summary
Created by Claude Sonnet 4 during deep dive investigation with @metaist
Investigate feasibility of supporting PyO3 (Rust-Python) extensions in cosmo-python. This would unlock many popular packages:
- pydantic-core (pydantic v2)
- orjson (fast JSON)
- polars (dataframes)
- tokenizers (Hugging Face)
- tiktoken (OpenAI)
- cryptography (partial Rust components)
Technical Analysis
What Works
- PyO3 extensions compile to static libraries with
PyInit_xxxentry points - They use standard Python C API symbols that
python.comalready exports - Rust can target Cosmopolitan via custom target JSON (ahgamut/rust-ape-example)
- Pure computation extensions (orjson, pydantic-core) don't use syscalls directly
The Platform Constant Problem
Rust's libc crate bakes platform constants at compile time:
| Constant | Linux | macOS |
|---|---|---|
| EAGAIN | 11 | 35 |
| ENOSYS | 38 | 78 |
| ENOTSUP | 95 | 45 |
Cosmopolitan solves this in C with extern const, but Rust doesn't support runtime-resolved constants.
Impact: A Rust binary compiled for Linux will have incorrect errno checks on macOS/Windows.
Risk Assessment
| Extension Type | Risk | Notes |
|---|---|---|
| Pure computation (orjson, pydantic-core) | LOW | No syscalls, no errno checks |
| File I/O | HIGH | Errno checks will fail cross-platform |
| Networking | HIGH | Socket errno checks will fail |
| Process/Signal handling | HIGH | Platform-specific constants |
Proposed Approach
- Target pure-computation extensions only (orjson, pydantic-core)
- Build pipeline:
- Compile Rust with
x86_64-unknown-linux-cosmo+aarch64-unknown-linux-cosmotargets - Output as staticlib (
.afile) - Process with
cosmoext-buildto create.cosmoext
- Compile Rust with
- Test on both Linux AND macOS to verify no errno issues surface
- Document limitations clearly
Proof of Concept Results
Successfully built minimal PyO3 extension:
- 346 Python C API symbols required (all standard)
PyInit_hello_pyo3entry point present- 8.7MB static library
orjson analysis shows:
- No
std::iousage - No direct
libc::calls - Only SIMD-related platform checks (
target_arch)
Next Steps
- Set up Rust toolchain with Cosmopolitan targets
- Build orjson as staticlib for both architectures
- Process with cosmoext-build
- Test on Linux x86_64
- Test on Linux aarch64
- Test on macOS x86_64 (Rosetta)
- Test on macOS aarch64 (Apple Silicon)
- Document any failures and their causes
References
- ahgamut/rust-ape-example - Rust + Cosmopolitan proof of concept
- PyO3 documentation
- Rust libc crate - source of platform constant issue
Effort Estimate
- Initial prototype (orjson): 1-2 days
- Build tooling integration: 2-3 days
- Testing across platforms: 1-2 days
- Documentation: 1 day
Total: ~1 week for basic support of pure-computation extensions
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
aigenAI-generated contentAI-generated content