Skip to content

iostream

Stream wrapper for file descriptors and socket-like objects.

This module provides IOStream, a wrapper that normalizes read/write access across different types of file-like objects (files, sockets, etc.) by detecting available methods at construction time.

IOStream

Wrapper that normalizes read/write operations on file-like objects.

Detects whether the underlying object has write/read methods (like files and sockets) or uses os.write/os.read (like raw file descriptors), and exposes a consistent .write() and .read() interface.

Example
import io

buf = io.BytesIO(b'payload')
data = iostream.IOStream(buf).read(4096)  # b'payload'

Attributes:

Name Type Description
write

A callable for writing data.

read

A callable for reading data.

Source code in py_misc_utils/iostream.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class IOStream:
  """Wrapper that normalizes read/write operations on file-like objects.

  Detects whether the underlying object has write/read methods (like files
  and sockets) or uses os.write/os.read (like raw file descriptors), and
  exposes a consistent .write() and .read() interface.

  Example:
    import io

    buf = io.BytesIO(b'payload')
    data = iostream.IOStream(buf).read(4096)  # b'payload'

  Attributes:
    write: A callable for writing data.
    read: A callable for reading data.
  """

  def __init__(self, fd):
    """Initialize the IOStream wrapping a file descriptor or file-like object.

    Args:
      fd: A file descriptor (int), file-like object with write/read methods,
        or socket-like object with send/recv methods.
    """
    if (wfn := getattr(fd, 'write', None)) is not None or (wfn := getattr(
        fd, 'send', None)) is not None:
      self.write = wfn
    else:
      self.write = functools.partial(os.write, fd)

    if (rfn := getattr(fd, 'read', None)) is not None or (rfn := getattr(
        fd, 'recv', None)) is not None:
      self.read = rfn
    else:
      self.read = functools.partial(os.read, fd)