Add Python bindings (Cython, zero-copy)#21
Open
ben-kaye wants to merge 2 commits into
Open
Conversation
Cython extension wrapping the lite3 C library, with packaging (setup.py/pyproject.toml), tests, and README.
- Check return codes on all l3_* writes (_chk); raise on buffer-full/overflow
- Raise OverflowError on ints outside int64 range instead of silent C wrap
- Block mutation while a memoryview is exported (BufferError)
- Invalidate stale sub-views after a realloc via a write generation counter;
views that did the write stay valid
- Add __repr__/__eq__/__hash__ on Lite3 and cheap reprs on _ObjView/_ArrView
- Ship PEP 561 stubs (__init__.pyi, py.typed) via pyproject package-data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add Python bindings
This adds an optional Python binding under
bindings/python/. It's a Cython wrapper around the existing C library, so nothing in the core changes. You read fields directly out of the serialized buffer through lazy dict/list-style proxies (no full copy), and writes are typed.memoryview(msg)is the wire format.Why a C shim
Most of the API lives in
include/lite3_context_api.has macros andstatic inlinefunctions, so it never produces linkable symbols.src/lite3_shim.cincludes the headers (macros expand in C) and re-exports them as plain extern functions the Cython layer calls. This is the standard approach and it keeps all the library logic in your C, with nothing duplicated in Python.Layout
The build compiles the repo's C sources directly (vendored into a gitignored
_vendor/at build time). It doesn't touch the Makefile, so your existing build is untouched.Notes
bindings/python/.OverflowErroron ints outside i64 range instead of a silent wrap, mutation blocked while a memoryview is exported, stale proxies invalidated after a realloc, and PEP 561 type stubs.8fee994. Cross-version buffer compatibility isn't guaranteed, which matters for the defrag/GC-index and formal-spec roadmap items.bytesonly round-trips losslessly through a typed write plusfrom_bytes.from_dict/to_dictbase64 it, since JSON has no bytes type.Testing