Summary
_consume_stream_without_lines (cuprum/_streams.py:40-65) and
_consume_stream_with_lines (cuprum/_streams.py:68-110) are near-duplicates.
They share the stream is None guard, the buffer setup, the read/echo loop
(read(_READ_SIZE) -> extend -> _write_chunk on echo), and the final
buffer.decode(...). The _with_lines variant only layers incremental-decoder
line emission on top.
Problem
A fix to the read/echo loop in one variant is easy to forget in the other,
silently diverging the capture path from the line-emitting path.
Proposed resolution — single canonical implementation
- Factor the shared mechanics into one canonical internal coroutine, for
example _drain(stream, config, *, on_chunk=None), that owns the read/echo/
buffer loop. _consume_stream_with_lines supplies an on_chunk callback that
feeds the incremental decoder; the no-lines variant supplies none.
- Keep the public
_consume_stream dispatch (_streams.py:28-37) as the single
entry point.
- Document the canonical drain helper and its re-use policy in
docs/developers-guide.md (indexed via docs/contents.md).
Verification
- Property tests (hypothesis): over arbitrary byte streams chunked at
arbitrary boundaries (including split multi-byte UTF-8 sequences and invalid
bytes), assert the canonical drain produces identical captured output to the
pre-refactor functions and that line emission is boundary-insensitive. Align
with the existing stream property-test suite.
- Model checking / snapshot assertions: n/a.
References
- Cuprum refactoring audit — consistency / near-duplicate consume functions.
Summary
_consume_stream_without_lines(cuprum/_streams.py:40-65) and_consume_stream_with_lines(cuprum/_streams.py:68-110) are near-duplicates.They share the
stream is Noneguard, thebuffersetup, the read/echo loop(
read(_READ_SIZE)->extend->_write_chunkon echo), and the finalbuffer.decode(...). The_with_linesvariant only layers incremental-decoderline emission on top.
Problem
A fix to the read/echo loop in one variant is easy to forget in the other,
silently diverging the capture path from the line-emitting path.
Proposed resolution — single canonical implementation
example
_drain(stream, config, *, on_chunk=None), that owns the read/echo/buffer loop.
_consume_stream_with_linessupplies anon_chunkcallback thatfeeds the incremental decoder; the no-lines variant supplies none.
_consume_streamdispatch (_streams.py:28-37) as the singleentry point.
docs/developers-guide.md(indexed viadocs/contents.md).Verification
arbitrary boundaries (including split multi-byte UTF-8 sequences and invalid
bytes), assert the canonical drain produces identical captured output to the
pre-refactor functions and that line emission is boundary-insensitive. Align
with the existing stream property-test suite.
References