Skip to content

add: PyO3/Rust extension support investigation #116

@metaist

Description

@metaist

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

  1. PyO3 extensions compile to static libraries with PyInit_xxx entry points
  2. They use standard Python C API symbols that python.com already exports
  3. Rust can target Cosmopolitan via custom target JSON (ahgamut/rust-ape-example)
  4. 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

  1. Target pure-computation extensions only (orjson, pydantic-core)
  2. Build pipeline:
    • Compile Rust with x86_64-unknown-linux-cosmo + aarch64-unknown-linux-cosmo targets
    • Output as staticlib (.a file)
    • Process with cosmoext-build to create .cosmoext
  3. Test on both Linux AND macOS to verify no errno issues surface
  4. Document limitations clearly

Proof of Concept Results

Successfully built minimal PyO3 extension:

  • 346 Python C API symbols required (all standard)
  • PyInit_hello_pyo3 entry point present
  • 8.7MB static library

orjson analysis shows:

  • No std::io usage
  • No direct libc:: calls
  • Only SIMD-related platform checks (target_arch)

Next Steps

  1. Set up Rust toolchain with Cosmopolitan targets
  2. Build orjson as staticlib for both architectures
  3. Process with cosmoext-build
  4. Test on Linux x86_64
  5. Test on Linux aarch64
  6. Test on macOS x86_64 (Rosetta)
  7. Test on macOS aarch64 (Apple Silicon)
  8. Document any failures and their causes

References

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    aigenAI-generated content

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions