Skip to content

Add Chapters 5-8: CLI Tools, FFI & PyO3, PyO3 House Style, Escaping the GIL#10

Merged
hartsock merged 5 commits into
mainfrom
chapters-05-08
Jun 8, 2026
Merged

Add Chapters 5-8: CLI Tools, FFI & PyO3, PyO3 House Style, Escaping the GIL#10
hartsock merged 5 commits into
mainfrom
chapters-05-08

Conversation

@hartsock

@hartsock hartsock commented Jun 6, 2026

Copy link
Copy Markdown
Member

Summary

Writes the four roadmap chapters that complete the course through Chapter 8 — the heart of the Python ↔ Rust interop arc. One commit per chapter, each following the ch03/ch04 template (annotated lib.rs with Python-comparison blocks + inline tests, exercises/ crate with todo!() stubs, chapter README):

  • Ch5 — CLI Tools (argparse/clickclap derive): typed args as parser-is-the-type-checker, ValueEnum, subcommand enums with exhaustive dispatch, --json structured output, doc-comments-become---help, exit codes. 27 tests, 5 exercises (17 stub tests).
  • Ch6 — FFI & PyO3 (ctypes#[pyfunction]/#[pymodule]/#[pyclass]): raw C-ABI pain first (with a # Safety contract), then PyO3 conversions, PyResult error mapping, and the maturin/abi3 wheel workflow. 20 tests, 5 exercises.
  • Ch7 — PyO3 House Style: the org's "Rust core with Python reach" operationalized — feature-gated bindings layer, one error enum mapped to typed Python exceptions at the boundary, zero-copy borrows proven by pointer-equality tests, one source of truth for both ecosystems. 19 tests, 5 exercises.
  • Ch8 — Escaping the GIL (threading/multiprocessingSend/Sync/rayon): scoped threads, two compile_fail doctests proving the data races don't compile, Arc<Mutex> vs channels (mapped to queue.Queue), par_iter, and py.allow_threads for the FFI payoff. 15 tests + 2 doctests, 5 exercises.

Design choice baked in (and taught explicitly in ch6/ch7): all PyO3 code sits behind a default-off python feature, so plain cargo test/clippy on CI needs no Python interpreter — exactly how real mixed crates ship. The gated code was verified to compile and pass clippy with --features python against Python 3.12 locally.

Final commit moves chapters 5–8 from the README roadmap table to the shipped chapters table (9–10 remain planned).

Test plan

  • CI-parity run on the integrated branch: cargo fmt --all -- --check ✅; cargo clippy --workspace --all-targets -- -D warnings ✅; cargo test --workspace excluding -exercises crates: 137 passed, 0 failed (+2 compile_fail doctests) ✅
  • Each chNN-exercises crate fails with todo! panics as designed; every chapter's exercises were proven solvable by the authors (solved, verified passing, reverted to stubs).
  • Safety: forbidden-pattern greps (private repos, hostnames, /home/<user>/ paths) clean on all new content; CI's secret-scan/forbidden-pattern jobs gate the rest.

🤖 Generated with Claude Code

hartsock and others added 5 commits June 5, 2026 20:07
Maps argparse/click to clap's derive API: the parser is a type, so
typed arguments, ValueEnum choices, and subcommand enums make invalid
invocations unrepresentable. Covers structured --json output via serde,
doc comments as --help text, exit-code mapping, and in-process CLI
testing with try_parse_from. Five "translate this Python" exercises.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Pure-Rust text-stats core with 20 inline tests; the PyO3 binding layer
(pyfunction/pyclass/pymodule, error mapping to ValueError) sits behind an
off-by-default 'python' feature so CI never needs a Python interpreter.
Exercises practice C-ABI boundaries, conversion shapes, and exception-ready
error types in pure Rust.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…g, zero-copy)

Operationalizes the Rust-core-with-Python-reach style: pyo3 sits behind a
default-off 'python' feature so cargo test stays pure Rust (the CI
constraint is the lesson), one error enum maps exhaustively to typed
Python exceptions, and zero-copy parsing shows what crosses the FFI line.
19 passing example tests; 5 exercises (19 tests) covering error design,
exception mapping, borrow-don't-clone, and Pythonic API shape.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Maps Python's GIL/threading/multiprocessing model to Rust's compile-time
concurrency: scoped threads, Send/Sync (with compile_fail doctests showing
data races the compiler rejects), Arc<Mutex<T>>, mpsc channels, and rayon.
PyO3 allow_threads example is gated behind a default-off `python` feature.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@hartsock
hartsock merged commit b4cbdb4 into main Jun 8, 2026
3 checks passed
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.

1 participant