Change ContainerIO return type to match file object mode#4297
Merged
hugovk merged 3 commits intopython-pillow:masterfrom Feb 15, 2020
Merged
Change ContainerIO return type to match file object mode#4297hugovk merged 3 commits intopython-pillow:masterfrom
hugovk merged 3 commits intopython-pillow:masterfrom
Conversation
jdufresne
reviewed
Jan 4, 2020
| break | ||
| s = s + c | ||
| if c == b"\n": | ||
| if c in [b"\n", "\n"]: |
Contributor
There was a problem hiding this comment.
This line results in a BytesWarning when running with the Python -b argument:
Tests/test_file_tar.py::TestFileTar::test_sanity
.../Pillow/.tox/py38/lib/python3.8/site-packages/PIL/ContainerIO.py:101: BytesWarning: Comparison between bytes and string
if c in [b"\n", "\n"]:
Member
Author
There was a problem hiding this comment.
Thanks. I've updated the commit.
Contributor
|
Overall, the approach makes sense. Do you have a use case in mind where the file would be opened in text mode? |
Member
Author
|
No. Feel free to argue that bytes mode is correct. I'm just thinking about backwards compatibility. |
jdufresne
approved these changes
Jan 18, 2020
| break | ||
| s = s + c | ||
| if c == "\n": | ||
| if c == (b"\n" if "b" in self.fh.mode else "\n"): |
Contributor
There was a problem hiding this comment.
This could be pulled out of the loop so it doesn't need to be recalculated every iteration of the loop.
Member
Author
There was a problem hiding this comment.
Good idea. I've pushed a commit for this.
hugovk
approved these changes
Feb 15, 2020
Image data is expected to be read in bytes mode, not text mode so ContainerIO should return bytes in all methods. The passed in file handler is expected to be opened in bytes mode (as TarIO already does).
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Alternative to #4192
That PR changes ContainerIO to return bytes instead of strings.
This PR changes ContainerIO to return strings if the file object is a string object, and bytes if the file object is a bytestring object.