Similar to ``http.client.HTTPResponse.read1`` and documented in :meth:`io.BufferedReader.read1`, but with an additional parameter: ``decode_content``. :param amt: How much of the content to read. :param decode_content: If True, will
(
self,
amt: int | None = None,
decode_content: bool | None = None,
)
| 1165 | return data |
| 1166 | |
| 1167 | def read1( |
| 1168 | self, |
| 1169 | amt: int | None = None, |
| 1170 | decode_content: bool | None = None, |
| 1171 | ) -> bytes: |
| 1172 | """ |
| 1173 | Similar to ``http.client.HTTPResponse.read1`` and documented |
| 1174 | in :meth:`io.BufferedReader.read1`, but with an additional parameter: |
| 1175 | ``decode_content``. |
| 1176 | |
| 1177 | :param amt: |
| 1178 | How much of the content to read. |
| 1179 | |
| 1180 | :param decode_content: |
| 1181 | If True, will attempt to decode the body based on the |
| 1182 | 'content-encoding' header. |
| 1183 | """ |
| 1184 | if decode_content is None: |
| 1185 | decode_content = self.decode_content |
| 1186 | if amt and amt < 0: |
| 1187 | # Negative numbers and `None` should be treated the same. |
| 1188 | amt = None |
| 1189 | # try and respond without going to the network |
| 1190 | if self._has_decoded_content: |
| 1191 | if not decode_content: |
| 1192 | raise RuntimeError( |
| 1193 | "Calling read1(decode_content=False) is not supported after " |
| 1194 | "read1(decode_content=True) was called." |
| 1195 | ) |
| 1196 | if ( |
| 1197 | self._decoder |
| 1198 | and self._decoder.has_unconsumed_tail |
| 1199 | and (amt is None or len(self._decoded_buffer) < amt) |
| 1200 | ): |
| 1201 | decoded_data = self._decode( |
| 1202 | b"", |
| 1203 | decode_content, |
| 1204 | flush_decoder=False, |
| 1205 | max_length=( |
| 1206 | amt - len(self._decoded_buffer) if amt is not None else None |
| 1207 | ), |
| 1208 | ) |
| 1209 | self._decoded_buffer.put(decoded_data) |
| 1210 | if len(self._decoded_buffer) > 0: |
| 1211 | if amt is None: |
| 1212 | return self._decoded_buffer.get_all() |
| 1213 | return self._decoded_buffer.get(amt) |
| 1214 | if amt == 0: |
| 1215 | return b"" |
| 1216 | |
| 1217 | # FIXME, this method's type doesn't say returning None is possible |
| 1218 | data = self._raw_read(amt, read1=True) |
| 1219 | self._uncached_read_occurred = True |
| 1220 | if not decode_content or data is None: |
| 1221 | return data |
| 1222 | |
| 1223 | self._init_decoder() |
| 1224 | while True: |