Skip to content

Refactor: canonical stream-drain loop shared by _consume_stream_* variants #115

Description

@lodyai

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions