Skip to content

Commit

Permalink
Correct str/bytes mixup in ContainerIO
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
jdufresne committed Nov 3, 2019
1 parent 69a5187 commit 60d6b6e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
40 changes: 20 additions & 20 deletions Tests/test_file_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_isatty(self):
def test_seek_mode_0(self):
# Arrange
mode = 0
with open(TEST_FILE) as fh:
with open(TEST_FILE, "rb") as fh:
container = ContainerIO.ContainerIO(fh, 22, 100)

# Act
Expand All @@ -32,7 +32,7 @@ def test_seek_mode_0(self):
def test_seek_mode_1(self):
# Arrange
mode = 1
with open(TEST_FILE) as fh:
with open(TEST_FILE, "rb") as fh:
container = ContainerIO.ContainerIO(fh, 22, 100)

# Act
Expand All @@ -45,7 +45,7 @@ def test_seek_mode_1(self):
def test_seek_mode_2(self):
# Arrange
mode = 2
with open(TEST_FILE) as fh:
with open(TEST_FILE, "rb") as fh:
container = ContainerIO.ContainerIO(fh, 22, 100)

# Act
Expand All @@ -57,64 +57,64 @@ def test_seek_mode_2(self):

def test_read_n0(self):
# Arrange
with open(TEST_FILE) as fh:
with open(TEST_FILE, "rb") as fh:
container = ContainerIO.ContainerIO(fh, 22, 100)

# Act
container.seek(81)
data = container.read()

# Assert
self.assertEqual(data, "7\nThis is line 8\n")
self.assertEqual(data, b"7\nThis is line 8\n")

def test_read_n(self):
# Arrange
with open(TEST_FILE) as fh:
with open(TEST_FILE, "rb") as fh:
container = ContainerIO.ContainerIO(fh, 22, 100)

# Act
container.seek(81)
data = container.read(3)

# Assert
self.assertEqual(data, "7\nT")
self.assertEqual(data, b"7\nT")

def test_read_eof(self):
# Arrange
with open(TEST_FILE) as fh:
with open(TEST_FILE, "rb") as fh:
container = ContainerIO.ContainerIO(fh, 22, 100)

# Act
container.seek(100)
data = container.read()

# Assert
self.assertEqual(data, "")
self.assertEqual(data, b"")

def test_readline(self):
# Arrange
with open(TEST_FILE) as fh:
with open(TEST_FILE, "rb") as fh:
container = ContainerIO.ContainerIO(fh, 0, 120)

# Act
data = container.readline()

# Assert
self.assertEqual(data, "This is line 1\n")
self.assertEqual(data, b"This is line 1\n")

def test_readlines(self):
# Arrange
expected = [
"This is line 1\n",
"This is line 2\n",
"This is line 3\n",
"This is line 4\n",
"This is line 5\n",
"This is line 6\n",
"This is line 7\n",
"This is line 8\n",
b"This is line 1\n",
b"This is line 2\n",
b"This is line 3\n",
b"This is line 4\n",
b"This is line 5\n",
b"This is line 6\n",
b"This is line 7\n",
b"This is line 8\n",
]
with open(TEST_FILE) as fh:
with open(TEST_FILE, "rb") as fh:
container = ContainerIO.ContainerIO(fh, 0, 120)

# Act
Expand Down
12 changes: 6 additions & 6 deletions docs/handbook/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -468,17 +468,17 @@ Reading from an open file
with open("hopper.ppm", "rb") as fp:
im = Image.open(fp)

To read an image from string data, use the :py:class:`~StringIO.StringIO`
class:
To read an image from bytes data, use the :py:class:`io.BytesIO` class:

Reading from a string
^^^^^^^^^^^^^^^^^^^^^
Reading from a byte string
^^^^^^^^^^^^^^^^^^^^^^^^^^

::

import io
from PIL import Image
import StringIO
im = Image.open(StringIO.StringIO(buffer))

im = Image.open(io.BytesIO(buffer))

Note that the library rewinds the file (using ``seek(0)``) before reading the
image header. In addition, seek will also be used when the image data is read
Expand Down
6 changes: 3 additions & 3 deletions src/PIL/ContainerIO.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def read(self, n=0):
else:
n = self.length - self.pos
if not n: # EOF
return ""
return b""
self.pos = self.pos + n
return self.fh.read(n)

Expand All @@ -92,13 +92,13 @@ def readline(self):
:returns: An 8-bit string.
"""
s = ""
s = b""
while True:
c = self.read(1)
if not c:
break
s = s + c
if c == "\n":
if c == b"\n":
break
return s

Expand Down

0 comments on commit 60d6b6e

Please sign in to comment.