Skip to content

Harden the C bridge; add stub, wheel, and type-check tooling - #13

Merged
kiranandcode merged 2 commits into
mainfrom
ffi-hardening-and-tooling
Aug 10, 2026
Merged

Harden the C bridge; add stub, wheel, and type-check tooling#13
kiranandcode merged 2 commits into
mainfrom
ffi-hardening-and-tooling

Conversation

@kiranandcode

Copy link
Copy Markdown
Collaborator

Cleans up the FFI layer and adds three tools borrowed from leanprover/nerodia.

FFI + cleanup. The C bridge now takes the GIL around every CPython-touching entry point, via a cleanup-attribute macro that releases it on all return paths. Correctness no longer rests on loading through ctypes PyDLL alone; it holds for foreign threads and free-threaded builds too. Removed lean_types.py and its manual m_rc helpers, which duplicated the refcount bug fixed earlier, and routed library.py through lean_dec. Swapped the magic 249/18 tags in format_lean_io_error for lean_is_string. Cleared a stale worktree, editor autosave files, and dead .pyc; corrected the README test count.

Type stubs. python -m lean_py.stubgen <project> <Lib> (or lib.write_stub()) generates a .pyi from the registry, so editors and type-checkers see real signatures instead of Any. mypy-clean on the 90-function TestLib surface.

Self-contained wheels. python -m lean_py.packaging build ... vendors the dylib closure plus lean.h, relocates them to @loader_path/$ORIGIN, and writes a py3-none-<platform> wheel with a loader. lean.py binds through ctypes, so no abi3 tag is needed. Two tests load the library with elan stripped from the environment.

Unified types. TypeRepr yields both the stub annotation (which stubgen consumes) and a runtime predicate (matches/check), with opt-in set_argument_typechecking(True). One description drives static hints and value checks, adapting nerodia's Typing = Py.Raw → Prop to the registry design.

1325 tests pass, none skipped, with sympy and numpy installed.

🤖 Generated with Claude Code

kiranandcode and others added 2 commits August 10, 2026 18:40
- GIL: acquire around every CPython-touching bridge entry via a
  cleanup-attribute WITH_GIL macro (releases on all return paths).
- Remove dead lean_types.py + manual m_rc helpers; route library.py
  through lean_dec. Replace magic IO.Error tag constants with
  lean_is_string. Clear stale worktree/autosave/pyc; fix README count.
- stubgen: generate .pyi from the registry (mypy-clean).
- packaging: vendor the dylib closure + lean.h into a py3-none wheel
  that loads with no toolchain present.
- registry: TypeRepr drives both the stub annotation and a runtime
  predicate (matches/check); opt-in set_argument_typechecking.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@kiranandcode
kiranandcode merged commit 5c972d1 into main Aug 10, 2026
12 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