Skip to content

Add PyBuffer tests, documentation, and async I/O support#30

Merged
benoitc merged 6 commits intomainfrom
feature/py-buffer-docs
Mar 14, 2026
Merged

Add PyBuffer tests, documentation, and async I/O support#30
benoitc merged 6 commits intomainfrom
feature/py-buffer-docs

Conversation

@benoitc
Copy link
Owner

@benoitc benoitc commented Mar 14, 2026

Summary

  • Add test suite for PyBuffer with 15 test cases
  • Add documentation for Buffer API
  • Add example script demonstrating all features
  • Add non-blocking read methods for async I/O integration
  • Update CHANGELOG and getting-started docs

Non-blocking methods added

  • read_nonblock(size=-1) - Read available bytes immediately
  • readable_amount() - Bytes available without blocking
  • at_eof() - Check if at EOF with no data remaining

Tests

Tests include Erlang streaming data while Python reads asynchronously, verifying the producer-consumer pattern works correctly.

benoitc added 6 commits March 14, 2026 21:12
- 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
@benoitc benoitc merged commit 9e651ee into main Mar 14, 2026
11 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