-
Notifications
You must be signed in to change notification settings - Fork 167
feat: add ignore_flush parameter to BlobWriter #644
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -229,11 +229,23 @@ class BlobWriter(io.BufferedIOBase): | |
| writes must be exactly a multiple of 256KiB as with other resumable | ||
| uploads. The default is the chunk_size of the blob, or 40 MiB. | ||
|
|
||
| :type text_mode: boolean | ||
| :type text_mode: bool | ||
| :param text_mode: | ||
| Whether this class is wrapped in 'io.TextIOWrapper'. Toggling this | ||
| changes the behavior of flush() to conform to TextIOWrapper's | ||
| expectations. | ||
| (Deprecated) A synonym for ignore_flush. For backwards-compatibility, | ||
| if True, sets ignore_flush to True. Use ignore_flush instead. This | ||
| parameter will be removed in a future release. | ||
|
|
||
| :type ignore_flush: bool | ||
| :param ignore_flush: | ||
| Makes flush() do nothing instead of raise an error. flush() without | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 to a handwritten doc for FileIO now that I'm looking at it. We could have more thorough explanations written on an individual page, and streamline the docstrings by linking to the page. |
||
| closing is not supported by the remote service and therefore calling it | ||
| on this class normally results in io.UnsupportedOperation. However, that | ||
| behavior is incompatible with some consumers and wrappers of file | ||
| objects in Python, such as zipfile.ZipFile or io.TextIOWrapper. Setting | ||
| ignore_flush will cause flush() to successfully do nothing, for | ||
| compatibility with those contexts. The correct way to actually flush | ||
| data to the remote server is to close() (using this object as a context | ||
| manager is recommended). | ||
|
|
||
| :type retry: google.api_core.retry.Retry or google.cloud.storage.retry.ConditionalRetryPolicy | ||
| :param retry: | ||
|
|
@@ -278,6 +290,7 @@ def __init__( | |
| blob, | ||
| chunk_size=None, | ||
| text_mode=False, | ||
| ignore_flush=False, | ||
| retry=DEFAULT_RETRY_IF_GENERATION_SPECIFIED, | ||
| **upload_kwargs | ||
| ): | ||
|
|
@@ -292,9 +305,8 @@ def __init__( | |
| # Resumable uploads require a chunk size of a multiple of 256KiB. | ||
| # self._chunk_size must not be changed after the upload is initiated. | ||
| self._chunk_size = chunk_size or blob.chunk_size or DEFAULT_CHUNK_SIZE | ||
| # In text mode this class will be wrapped and TextIOWrapper requires a | ||
| # different behavior of flush(). | ||
| self._text_mode = text_mode | ||
| # text_mode is a deprecated synonym for ignore_flush | ||
| self._ignore_flush = ignore_flush or text_mode | ||
| self._retry = retry | ||
| self._upload_kwargs = upload_kwargs | ||
|
|
||
|
|
@@ -394,13 +406,14 @@ def tell(self): | |
| return self._buffer.tell() + len(self._buffer) | ||
|
|
||
| def flush(self): | ||
| if self._text_mode: | ||
| # TextIOWrapper expects this method to succeed before calling close(). | ||
| return | ||
|
|
||
| raise io.UnsupportedOperation( | ||
| "Cannot flush without finalizing upload. Use close() instead." | ||
| ) | ||
| # flush() is not fully supported by the remote service, so raise an | ||
| # error here, unless self._ignore_flush is set. | ||
| if not self._ignore_flush: | ||
| raise io.UnsupportedOperation( | ||
| "Cannot flush without finalizing upload. Use close() instead, " | ||
| "or set ignore_flush=True when constructing this class (see " | ||
| "docstring)." | ||
| ) | ||
|
|
||
| def close(self): | ||
| self._checkClosed() # Raises ValueError if closed. | ||
|
|
||
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
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.
@andrewsg Should these instead be
if ignore_flush is not None:if
ignore_flush=Falseis accidentally set for reads, it would cause an AttributeError or such.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.
Good point to focus on. I thought about that but decided that since False was the actual desired behavior, we didn't need to guard against it here. I don't think it would cause an AttributeError - is there a place that looks suspicious?
Uh oh!
There was an error while loading. Please reload this page.
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.
I see what you mean. That's right; it wouldn't cause an error since
ignore_flushis not passed in to theBlobReaderconstructor after all. I misread that.