Repository navigation
Fix fsspec data loss after flush and reduce stream buffering costs #407
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
Open
shcheklein
wants to merge
2
commits into
main
Choose a base branch
from
fix/fsspec-stream-io
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+231
−71
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
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 |
|---|---|---|
| @@ -1,56 +1,65 @@ | ||
| import io | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| from collections.abc import Iterator | ||
|
|
||
|
|
||
| class IterStream(io.RawIOBase): | ||
| """Wraps an iterator yielding bytes as a file object""" | ||
| """Wraps an iterator yielding bytes as a file object.""" | ||
|
|
||
| def __init__(self, iterator): # pylint: disable=super-init-not-called | ||
| self.iterator = iterator | ||
| self.leftover = b"" | ||
| def __init__(self, iterator): | ||
| super().__init__() | ||
| self.iterator: Iterator[bytes] = filter(None, iterator) | ||
| self.leftover: bytes | memoryview = b"" | ||
|
|
||
| def readable(self): | ||
| return True | ||
|
|
||
| def writable(self) -> bool: | ||
| return False | ||
| def close(self): | ||
| self.leftover = b"" | ||
| self.iterator = iter(()) | ||
| super().close() | ||
|
|
||
| # Python 3 requires only .readinto() method, it still uses other ones | ||
| # under some circumstances and falls back if those are absent. Since | ||
| # iterator already constructs byte strings for us, .readinto() is not the | ||
| # most optimal, so we provide .read1() too. | ||
| def _take(self, size): | ||
| if size == 0: | ||
| return b"" | ||
| chunk = self.leftover or next(self.iterator, b"") | ||
| if size < 0 or size >= len(chunk): | ||
| self.leftover = b"" | ||
| return chunk | ||
| # Keep a view of the unread bytes until the response is exhausted. | ||
| view = memoryview(chunk) | ||
| self.leftover = view[size:] | ||
| return view[:size] | ||
|
|
||
| def readinto(self, b): | ||
| try: | ||
| n = len(b) # We're supposed to return at most this much | ||
| chunk = self.leftover or next(self.iterator) | ||
| output, self.leftover = chunk[:n], chunk[n:] | ||
|
|
||
| n_out = len(output) | ||
| b[:n_out] = output | ||
| return n_out | ||
| except StopIteration: | ||
| return 0 # indicate EOF | ||
| self._checkClosed() | ||
| with memoryview(b).cast("B") as dest: | ||
| if dest.readonly: | ||
| raise TypeError("readinto() requires a writable buffer") | ||
| chunk = self._take(len(dest)) | ||
| dest[: len(chunk)] = chunk | ||
| return len(chunk) | ||
|
|
||
| readinto1 = readinto | ||
|
|
||
| def read1(self, n=-1): | ||
| try: | ||
| chunk = self.leftover or next(self.iterator) | ||
| except StopIteration: | ||
| return b"" | ||
|
|
||
| # Return an arbitrary number or bytes | ||
| if n <= 0: | ||
| self.leftover = b"" | ||
| return chunk | ||
|
|
||
| output, self.leftover = chunk[:n], chunk[n:] | ||
| return output | ||
| self._checkClosed() | ||
| return bytes(self._take(n)) | ||
|
|
||
| def peek(self, n): | ||
| while len(self.leftover) < n: | ||
| self._checkClosed() | ||
| available = len(self.leftover) | ||
| if available < n: | ||
| chunks = [self.leftover] if available else [] | ||
| try: | ||
| self.leftover += next(self.iterator) | ||
| except StopIteration: | ||
| break | ||
| return self.leftover[:n] | ||
| for chunk in self.iterator: | ||
| chunks.append(chunk) | ||
| available += len(chunk) | ||
| if available >= n: | ||
| break | ||
| finally: | ||
| # Preserve fetched bytes even if a later request fails. | ||
| self.leftover = b"".join(chunks) | ||
| return bytes(self.leftover[:n]) |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.