MCPcopy
hub / github.com/django/django / LimitedStream

Class LimitedStream

django/core/handlers/wsgi.py:15–53  ·  view source on GitHub ↗

Wrap another stream to disallow reading it past a number of bytes. Based on the implementation from werkzeug.wsgi.LimitedStream. See: https://github.com/pallets/werkzeug/blob/dbf78f67/src/werkzeug/wsgi.py#L828

Source from the content-addressed store, hash-verified

13
14
15class LimitedStream(IOBase):
16 """
17 Wrap another stream to disallow reading it past a number of bytes.
18
19 Based on the implementation from werkzeug.wsgi.LimitedStream. See:
20 https://github.com/pallets/werkzeug/blob/dbf78f67/src/werkzeug/wsgi.py#L828
21 """
22
23 def __init__(self, stream, limit):
24 self._read = stream.read
25 self._readline = stream.readline
26 self._pos = 0
27 self.limit = limit
28
29 def read(self, size=-1, /):
30 _pos = self._pos
31 limit = self.limit
32 if _pos >= limit:
33 return b""
34 if size == -1 or size is None:
35 size = limit - _pos
36 else:
37 size = min(size, limit - _pos)
38 data = self._read(size)
39 self._pos += len(data)
40 return data
41
42 def readline(self, size=-1, /):
43 _pos = self._pos
44 limit = self.limit
45 if _pos >= limit:
46 return b""
47 if size == -1 or size is None:
48 size = limit - _pos
49 else:
50 size = min(size, limit - _pos)
51 line = self._readline(size)
52 self._pos += len(line)
53 return line
54
55
56class WSGIRequest(HttpRequest):

Callers 6

__init__Method · 0.90
__call__Method · 0.90
requestMethod · 0.90
test_limited_streamMethod · 0.90
__init__Method · 0.90
__init__Method · 0.85

Calls

no outgoing calls

Tested by 2

test_limited_streamMethod · 0.72
__init__Method · 0.72