Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.3k
Support for chunk_size#1277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c1d56dc
Support iter_raw(chunk_size=...) and aiter_raw(chunk_size=...)
lovelydinosaur 2b5d116
Unit tests for ByteChunker
lovelydinosaur 8b6034f
Support iter_bytes(chunk_size=...)
lovelydinosaur 7c40709
Add TextChunker
lovelydinosaur bff7dbb
Support iter_text(chunk_size=...)
lovelydinosaur f71e545
Merge branch 'master' into chunk-size
lovelydinosaur e9160c2
Fix merge with master
lovelydinosaur d3c8542
Merge branch 'master' into chunk-size
florimondmanca d61e384
Merge branch 'master' into chunk-size
lovelydinosaur File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -15,10 +15,12 @@ | ||
| from ._content import PlainByteStream, encode_request, encode_response | ||
| from ._decoders import ( | ||
| SUPPORTED_DECODERS, | ||
| ByteChunker, | ||
| ContentDecoder, | ||
| IdentityDecoder, | ||
| LineDecoder, | ||
| MultiDecoder, | ||
| TextChunker, | ||
| TextDecoder, | ||
| ) | ||
| from ._exceptions import ( | ||
| @@ -1162,31 +1164,47 @@ def read(self) -> bytes: | ||
| self._content = b"".join(self.iter_bytes()) | ||
| return self._content | ||
| def iter_bytes(self) -> typing.Iterator[bytes]: | ||
| def iter_bytes(self, chunk_size: int = None) -> typing.Iterator[bytes]: | ||
| """ | ||
| A byte-iterator over the decoded response content. | ||
| This allows us to handle gzip, deflate, and brotli encoded responses. | ||
| """ | ||
| if hasattr(self, "_content"): | ||
| yield self._content | ||
| chunk_size = len(self._content) if chunk_size is None else chunk_size | ||
| for i in range(0, len(self._content), chunk_size): | ||
| yield self._content[i : i + chunk_size] | ||
| else: | ||
| decoder = self._get_content_decoder() | ||
| chunker = ByteChunker(chunk_size=chunk_size) | ||
| with self._wrap_decoder_errors(): | ||
| for chunk in self.iter_raw(): | ||
| yield decoder.decode(chunk) | ||
| yield decoder.flush() | ||
| def iter_text(self) -> typing.Iterator[str]: | ||
| for raw_bytes in self.iter_raw(): | ||
| decoded = decoder.decode(raw_bytes) | ||
| for chunk in chunker.decode(decoded): | ||
| yield chunk | ||
| decoded = decoder.flush() | ||
| for chunk in chunker.decode(decoded): | ||
| yield chunk | ||
| for chunk in chunker.flush(): | ||
| yield chunk | ||
| def iter_text(self, chunk_size: int = None) -> typing.Iterator[str]: | ||
| """ | ||
| A str-iterator over the decoded response content | ||
| that handles both gzip, deflate, etc but also detects the content's | ||
| string encoding. | ||
| """ | ||
| decoder = TextDecoder(encoding=self.encoding) | ||
| chunker = TextChunker(chunk_size=chunk_size) | ||
| with self._wrap_decoder_errors(): | ||
| for chunk in self.iter_bytes(): | ||
| yield decoder.decode(chunk) | ||
| yield decoder.flush() | ||
| for byte_content in self.iter_bytes(): | ||
| text_content = decoder.decode(byte_content) | ||
| for chunk in chunker.decode(text_content): | ||
| yield chunk | ||
| text_content = decoder.flush() | ||
| for chunk in chunker.decode(text_content): | ||
| yield chunk | ||
| for chunk in chunker.flush(): | ||
| yield chunk | ||
| def iter_lines(self) -> typing.Iterator[str]: | ||
| decoder = LineDecoder() | ||
| @@ -1197,7 +1215,7 @@ def iter_lines(self) -> typing.Iterator[str]: | ||
| for line in decoder.flush(): | ||
| yield line | ||
| def iter_raw(self) -> typing.Iterator[bytes]: | ||
| def iter_raw(self, chunk_size: int = None) -> typing.Iterator[bytes]: | ||
| """ | ||
| A byte-iterator over the raw response content. | ||
| """ | ||
| @@ -1210,10 +1228,17 @@ def iter_raw(self) -> typing.Iterator[bytes]: | ||
| self.is_stream_consumed = True | ||
| self._num_bytes_downloaded = 0 | ||
| chunker = ByteChunker(chunk_size=chunk_size) | ||
| with map_exceptions(HTTPCORE_EXC_MAP, request=self._request): | ||
| for part in self.stream: | ||
| self._num_bytes_downloaded += len(part) | ||
| yield part | ||
| for raw_stream_bytes in self.stream: | ||
| self._num_bytes_downloaded += len(raw_stream_bytes) | ||
| for chunk in chunker.decode(raw_stream_bytes): | ||
florimondmanca marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| yield chunk | ||
| for chunk in chunker.flush(): | ||
| yield chunk | ||
| self.close() | ||
| def close(self) -> None: | ||
| @@ -1234,31 +1259,47 @@ async def aread(self) -> bytes: | ||
| self._content = b"".join([part async for part in self.aiter_bytes()]) | ||
| return self._content | ||
| async def aiter_bytes(self) -> typing.AsyncIterator[bytes]: | ||
| async def aiter_bytes(self, chunk_size: int = None) -> typing.AsyncIterator[bytes]: | ||
| """ | ||
| A byte-iterator over the decoded response content. | ||
| This allows us to handle gzip, deflate, and brotli encoded responses. | ||
| """ | ||
| if hasattr(self, "_content"): | ||
| yield self._content | ||
| chunk_size = len(self._content) if chunk_size is None else chunk_size | ||
| for i in range(0, len(self._content), chunk_size): | ||
| yield self._content[i : i + chunk_size] | ||
| else: | ||
| decoder = self._get_content_decoder() | ||
| chunker = ByteChunker(chunk_size=chunk_size) | ||
| with self._wrap_decoder_errors(): | ||
| async for chunk in self.aiter_raw(): | ||
| yield decoder.decode(chunk) | ||
| yield decoder.flush() | ||
| async def aiter_text(self) -> typing.AsyncIterator[str]: | ||
| async for raw_bytes in self.aiter_raw(): | ||
| decoded = decoder.decode(raw_bytes) | ||
| for chunk in chunker.decode(decoded): | ||
| yield chunk | ||
| decoded = decoder.flush() | ||
| for chunk in chunker.decode(decoded): | ||
| yield chunk | ||
| for chunk in chunker.flush(): | ||
| yield chunk | ||
| async def aiter_text(self, chunk_size: int = None) -> typing.AsyncIterator[str]: | ||
| """ | ||
| A str-iterator over the decoded response content | ||
| that handles both gzip, deflate, etc but also detects the content's | ||
| string encoding. | ||
| """ | ||
| decoder = TextDecoder(encoding=self.encoding) | ||
| chunker = TextChunker(chunk_size=chunk_size) | ||
| with self._wrap_decoder_errors(): | ||
| async for chunk in self.aiter_bytes(): | ||
| yield decoder.decode(chunk) | ||
| yield decoder.flush() | ||
| async for byte_content in self.aiter_bytes(): | ||
| text_content = decoder.decode(byte_content) | ||
| for chunk in chunker.decode(text_content): | ||
| yield chunk | ||
| text_content = decoder.flush() | ||
| for chunk in chunker.decode(text_content): | ||
| yield chunk | ||
| for chunk in chunker.flush(): | ||
| yield chunk | ||
| async def aiter_lines(self) -> typing.AsyncIterator[str]: | ||
| decoder = LineDecoder() | ||
| @@ -1269,7 +1310,7 @@ async def aiter_lines(self) -> typing.AsyncIterator[str]: | ||
| for line in decoder.flush(): | ||
| yield line | ||
| async def aiter_raw(self) -> typing.AsyncIterator[bytes]: | ||
| async def aiter_raw(self, chunk_size: int = None) -> typing.AsyncIterator[bytes]: | ||
| """ | ||
| A byte-iterator over the raw response content. | ||
| """ | ||
| @@ -1282,10 +1323,17 @@ async def aiter_raw(self) -> typing.AsyncIterator[bytes]: | ||
| self.is_stream_consumed = True | ||
| self._num_bytes_downloaded = 0 | ||
| chunker = ByteChunker(chunk_size=chunk_size) | ||
| with map_exceptions(HTTPCORE_EXC_MAP, request=self._request): | ||
| async for part in self.stream: | ||
| self._num_bytes_downloaded += len(part) | ||
| yield part | ||
| async for raw_stream_bytes in self.stream: | ||
| self._num_bytes_downloaded += len(raw_stream_bytes) | ||
| for chunk in chunker.decode(raw_stream_bytes): | ||
| yield chunk | ||
| for chunk in chunker.flush(): | ||
| yield chunk | ||
| await self.aclose() | ||
| async def aclose(self) -> None: | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure about chunk_size with default
None?I do agree that it looks more suitable, but
requestsprovides us with defaults chunk_size=1 or 512There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this PR, when
chunk_size=Nonewe just return the inputcontentunchanged, as one single big chunk.Yes, this would deviate from what Requests seems to do, but:
Nonedefault would break backward compatibility on our side.That said, we'd need to add this deviation from Requests to the compatibility guide. 👍