MCPcopy
hub / github.com/scrapy/scrapy / _StreamReader

Class _StreamReader

scrapy/utils/iterators.py:124–155  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

122
123
124class _StreamReader:
125 def __init__(self, obj: Response | str | bytes):
126 self._ptr: int = 0
127 self._text: str | bytes
128 if isinstance(obj, TextResponse):
129 self._text, self.encoding = obj.body, obj.encoding
130 elif isinstance(obj, Response):
131 self._text, self.encoding = obj.body, "utf-8"
132 else:
133 self._text, self.encoding = obj, "utf-8"
134 self._is_unicode: bool = isinstance(self._text, str)
135 self._is_first_read: bool = True
136
137 def read(self, n: int = 65535) -> bytes:
138 method: Callable[[int], bytes] = (
139 self._read_unicode if self._is_unicode else self._read_string
140 )
141 result = method(n)
142 if self._is_first_read:
143 self._is_first_read = False
144 result = result.lstrip()
145 return result
146
147 def _read_string(self, n: int = 65535) -> bytes:
148 s, e = self._ptr, self._ptr + n
149 self._ptr = e
150 return cast("bytes", self._text)[s:e]
151
152 def _read_unicode(self, n: int = 65535) -> bytes:
153 s, e = self._ptr, self._ptr + n
154 self._ptr = e
155 return cast("str", self._text)[s:e].encode("utf-8")
156
157
158def csviter(

Callers 1

xmliter_lxmlFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected