forked from iterative/dvc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes iterative#3408 Related iterative#2865 Fixes iterative#3897
- Loading branch information
Showing
4 changed files
with
68 additions
and
45 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import io | ||
|
||
|
||
class IterStream(io.RawIOBase): | ||
"""Wraps an iterator yielding bytes as a file object""" | ||
|
||
def __init__(self, iterator): | ||
self.iterator = iterator | ||
self.leftover = None | ||
|
||
def readable(self): | ||
return True | ||
|
||
# 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 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 | ||
|
||
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 = None | ||
return chunk | ||
|
||
output, self.leftover = chunk[:n], chunk[n:] | ||
return output |
This file contains 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