OpenQuizz
Une application de gestion des contenus pédagogiques
LimitedStream Class Reference
Inheritance diagram for LimitedStream:
Collaboration diagram for LimitedStream:

Public Member Functions

def __init__ (self, stream, limit)
 
def __iter__ (self)
 
def is_exhausted (self)
 
def on_exhausted (self)
 
def on_disconnect (self)
 
def exhaust (self, chunk_size=1024 *64)
 
def read (self, size=None)
 
def readline (self, size=None)
 
def readlines (self, size=None)
 
def tell (self)
 
def __next__ (self)
 
def readable (self)
 

Data Fields

 limit
 

Detailed Description

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`)

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
  stream,
  limit 
)

Member Function Documentation

◆ __iter__()

def __iter__ (   self)

◆ __next__()

def __next__ (   self)

◆ exhaust()

def exhaust (   self,
  chunk_size = 1024 * 64 
)
Exhaust the stream.  This consumes all the data left until the
limit is reached.

:param chunk_size: the size for a chunk.  It will read the chunk
           until the stream is exhausted and throw away
           the results.

◆ is_exhausted()

def is_exhausted (   self)
If the stream is exhausted this attribute is `True`.

◆ on_disconnect()

def on_disconnect (   self)
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.

◆ on_exhausted()

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

◆ read()

def read (   self,
  size = None 
)
Read `size` bytes or if size is not provided everything is read.

:param size: the number of bytes read.

◆ readable()

def readable (   self)

◆ readline()

def readline (   self,
  size = None 
)
Reads one line from the stream.

◆ readlines()

def readlines (   self,
  size = None 
)
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`.

◆ tell()

def tell (   self)
Returns the position of the stream.

.. versionadded:: 0.9

Field Documentation

◆ limit

limit

The documentation for this class was generated from the following file: