Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Mar 31, 2026. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 173
feat: add ignore_flush parameter to BlobWriter#644
Merged
Uh oh!
There was an error while loading. Please reload this page.
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -3760,6 +3760,7 @@ def open( | ||
| self, | ||
| mode="r", | ||
| chunk_size=None, | ||
| ignore_flush=None, | ||
| encoding=None, | ||
| errors=None, | ||
| newline=None, | ||
| @@ -3801,6 +3802,19 @@ def open( | ||
| chunk_size for writes must be exactly a multiple of 256KiB as with | ||
| other resumable uploads. The default is 40 MiB. | ||
| :type ignore_flush: bool | ||
| :param ignore_flush: | ||
| (Optional) For non text-mode writes, makes flush() do nothing | ||
| instead of raising an error. flush() without closing is not | ||
| supported by the remote service and therefore calling it 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 a context manager, | ||
| such as in the example, will cause this to happen automatically). | ||
| :type encoding: str | ||
| :param encoding: | ||
| (Optional) For text mode only, the name of the encoding that the stream will | ||
| @@ -3873,23 +3887,38 @@ def open( | ||
| raise ValueError( | ||
| "encoding, errors and newline arguments are for text mode only" | ||
| ) | ||
| if ignore_flush: | ||
| raise ValueError( | ||
| "ignore_flush argument is for non-text write mode only" | ||
| ) | ||
tseaver marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| return BlobReader(self, chunk_size=chunk_size, **kwargs) | ||
| elif mode == "wb": | ||
| if encoding or errors or newline: | ||
| raise ValueError( | ||
| "encoding, errors and newline arguments are for text mode only" | ||
| ) | ||
| return BlobWriter(self, chunk_size=chunk_size, **kwargs) | ||
| return BlobWriter( | ||
| self, chunk_size=chunk_size, ignore_flush=ignore_flush, **kwargs | ||
| ) | ||
| elif mode in ("r", "rt"): | ||
| if ignore_flush: | ||
| raise ValueError( | ||
| "ignore_flush argument is for non-text write mode only" | ||
| ) | ||
| return TextIOWrapper( | ||
| BlobReader(self, chunk_size=chunk_size, **kwargs), | ||
| encoding=encoding, | ||
| errors=errors, | ||
| newline=newline, | ||
| ) | ||
| elif mode in ("w", "wt"): | ||
| if ignore_flush is False: | ||
| raise ValueError( | ||
| "ignore_flush is required for text mode writing and " | ||
| "cannot be set to False" | ||
| ) | ||
| return TextIOWrapper( | ||
| BlobWriter(self, chunk_size=chunk_size, text_mode=True, **kwargs), | ||
| BlobWriter(self, chunk_size=chunk_size, ignore_flush=True, **kwargs), | ||
| encoding=encoding, | ||
| errors=errors, | ||
| newline=newline, | ||
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.