Skip to content

Change ContainerIO return type to match file object mode#4297

Merged
hugovk merged 3 commits intopython-pillow:masterfrom
radarhere:container
Feb 15, 2020
Merged

Change ContainerIO return type to match file object mode#4297
hugovk merged 3 commits intopython-pillow:masterfrom
radarhere:container

Conversation

@radarhere
Copy link
Copy Markdown
Member

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.

Comment thread src/PIL/ContainerIO.py Outdated
break
s = s + c
if c == b"\n":
if c in [b"\n", "\n"]:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"]:

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I've updated the commit.

@jdufresne
Copy link
Copy Markdown
Contributor

Overall, the approach makes sense. Do you have a use case in mind where the file would be opened in text mode?

@radarhere
Copy link
Copy Markdown
Member Author

No. Feel free to argue that bytes mode is correct. I'm just thinking about backwards compatibility.

Comment thread src/PIL/ContainerIO.py Outdated
break
s = s + c
if c == "\n":
if c == (b"\n" if "b" in self.fh.mode else "\n"):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be pulled out of the loop so it doesn't need to be recalculated every iteration of the loop.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. I've pushed a commit for this.

jdufresne and others added 3 commits February 15, 2020 22:13
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).
@hugovk hugovk merged commit cc3c743 into python-pillow:master Feb 15, 2020
@radarhere radarhere deleted the container branch February 15, 2020 12:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants