Add PyBuffer tests, documentation, and async I/O support#30
Merged
Conversation
- test/py_buffer_SUITE.erl: Common Test suite with 13 tests for: - Basic buffer creation (with/without content length) - Write/read cycle, readline, readlines - Seek/tell position tracking - Fast find with memchr/memmem - Zero-copy memoryview access - Line iteration, closed/empty buffer handling - Auto-conversion when passing buffer ref to Python - GC and reference counting verification - docs/buffer.md: Documentation covering: - Erlang API (new, write, close) - Python API (file-like methods, buffer protocol, find) - Architecture diagram and memory layout - Performance tips and examples - examples/py_buffer_example.erl: Working escript demonstrating: - Basic buffer usage - HTTP body streaming simulation - File-like interface methods - Zero-copy memoryview access - Line iteration for CSV-like data - CHANGELOG.md: Added PyBuffer API entry under 2.2.0 - docs/getting-started.md: Added Zero-Copy Buffers section with link to buffer.md
- read_nonblock(size=-1): Read available bytes immediately, never blocks - readable_amount(): Return bytes available without blocking - at_eof(): Check if at EOF with no more data These methods enable async I/O patterns where Python code needs to check for available data without blocking, suitable for asyncio integration. Tests demonstrate Erlang streaming data while Python reads asynchronously.
Memory leaks:
- Py_BuildValue("()") in readlines and iternext was never DECREF'd
- Now uses PyTuple_New(0) with proper cleanup
Deadlock prevention:
- read/readline/read_nonblock now copy data while holding mutex,
then release mutex before calling Python APIs (PyBytes_FromStringAndSize)
- This avoids holding mutex while reacquiring GIL, which could deadlock
with other functions (find, readable_amount, etc.) that hold GIL
while acquiring mutex
Integer overflow:
- Added SIZE_MAX check before computing required capacity in py_buffer_write
- c_src/py_buffer.h: Header with resource struct and function declarations - src/py_buffer.erl: Erlang API module (new/0,1, write/2, close/1) - c_src/py_convert.c: Auto-conversion of buffer refs to PyBuffer objects - c_src/py_nif.c: NIF registration and resource type initialization - src/py_nif.erl: NIF function exports
PyBuffer was only registered in the main interpreter, causing ImportError when using subinterpreters (Python 3.12+). Add PyBuffer_register_with_module() calls in: - py_subinterp_pool.c: for shared-GIL subinterpreter pool - py_subinterp_thread.c: for OWN_GIL subinterpreter threads
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.
Summary
Non-blocking methods added
read_nonblock(size=-1)- Read available bytes immediatelyreadable_amount()- Bytes available without blockingat_eof()- Check if at EOF with no data remainingTests
Tests include Erlang streaming data while Python reads asynchronously, verifying the producer-consumer pattern works correctly.