class documentation

class LimitedStream(io.IOBase): (source)

View In Hierarchy

Wraps a stream so that it doesn't read more than n bytes. If the stream is exhausted and the caller tries to get more bytes from it :func:`on_exhausted` is called which by default returns an empty string. The return value of that function is forwarded to the reader function. So if it returns an empty string :meth:`read` will return an empty string as well. The limit however must never be higher than what the stream can output. Otherwise :meth:`readlines` will try to read past the limit. .. admonition:: Note on WSGI compliance calls to :meth:`readline` and :meth:`readlines` are not WSGI compliant because it passes a size argument to the readline methods. Unfortunately the WSGI PEP is not safely implementable without a size argument to :meth:`readline` because there is no EOF marker in the stream. As a result of that the use of :meth:`readline` is discouraged. For the same reason iterating over the :class:`LimitedStream` is not portable. It internally calls :meth:`readline`. We strongly suggest using :meth:`read` only or using the :func:`make_line_iter` which safely iterates line-based over a WSGI input stream. :param stream: the stream to wrap. :param limit: the limit for the stream, must not be longer than what the string can provide if the stream does not end with `EOF` (like `wsgi.input`)

Method __init__ Undocumented
Method __iter__ Undocumented
Method __next__ Undocumented
Method exhaust Exhaust the stream by reading until the limit is reached or the client disconnects, discarding the data.
Method on_disconnect What should happen if a disconnect is detected? The return value of this function is returned from read functions in case the client went away. By default a :exc:`~werkzeug.exceptions.ClientDisconnected` exception is raised.
Method on_exhausted This is called when the stream tries to read past the limit. The return value of this function is returned from the reading function.
Method read Read up to ``size`` bytes from the underlying stream. If size is not provided, read until the limit.
Method readable Undocumented
Method readline Reads one line from the stream.
Method readlines Reads a file into a list of strings. It calls :meth:`readline` until the file is read to the end. It does support the optional `size` argument if the underlying stream supports it for `readline`.
Method tell Returns the position of the stream.
Instance Variable limit Undocumented
Property is_exhausted If the stream is exhausted this attribute is `True`.
Method _exhaust_chunks Exhaust the stream by reading until the limit is reached or the client disconnects, yielding each chunk.
Instance Variable _pos Undocumented
Instance Variable _read Undocumented
Instance Variable _readline Undocumented
def __init__(self, stream: t.IO[bytes], limit: int): (source)

Undocumented

def __iter__(self) -> LimitedStream: (source)

Undocumented

def __next__(self) -> bytes: (source)

Undocumented

def exhaust(self, chunk_size: int = 1024*64): (source)

Exhaust the stream by reading until the limit is reached or the client disconnects, discarding the data. :param chunk_size: How many bytes to read at a time. .. versionchanged:: 2.2.3 Handle case where wrapped stream returns fewer bytes than requested.

def on_disconnect(self) -> bytes: (source)

What should happen if a disconnect is detected? The return value of this function is returned from read functions in case the client went away. By default a :exc:`~werkzeug.exceptions.ClientDisconnected` exception is raised.

def on_exhausted(self) -> bytes: (source)

This is called when the stream tries to read past the limit. The return value of this function is returned from the reading function.

def read(self, size: t.Optional[int] = None) -> bytes: (source)

Read up to ``size`` bytes from the underlying stream. If size is not provided, read until the limit. If the limit is reached, :meth:`on_exhausted` is called, which returns empty bytes. If no bytes are read and the limit is not reached, or if an error occurs during the read, :meth:`on_disconnect` is called, which raises :exc:`.ClientDisconnected`. :param size: The number of bytes to read. ``None``, default, reads until the limit is reached. .. versionchanged:: 2.2.3 Handle case where wrapped stream returns fewer bytes than requested.

def readable(self) -> bool: (source)

Undocumented

def readline(self, size: t.Optional[int] = None) -> bytes: (source)

Reads one line from the stream.

def readlines(self, size: t.Optional[int] = None) -> t.List[bytes]: (source)

Reads a file into a list of strings. It calls :meth:`readline` until the file is read to the end. It does support the optional `size` argument if the underlying stream supports it for `readline`.

def tell(self) -> int: (source)

Returns the position of the stream. .. versionadded:: 0.9

Undocumented

@property
is_exhausted: bool = (source)

If the stream is exhausted this attribute is `True`.

def _exhaust_chunks(self, chunk_size: int = 1024*64) -> t.Iterator[bytes]: (source)

Exhaust the stream by reading until the limit is reached or the client disconnects, yielding each chunk. :param chunk_size: How many bytes to read at a time. :meta private: .. versionadded:: 2.2.3

Undocumented

Undocumented

_readline = (source)

Undocumented