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 authored and radarhere committed Feb 15, 2020
1 parent 9174a45 commit 5426bbc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
40 changes: 20 additions & 20 deletions Tests/test_file_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_isatty():
def test_seek_mode_0():
# 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 @@ -34,7 +34,7 @@ def test_seek_mode_0():
def test_seek_mode_1():
# 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 @@ -48,7 +48,7 @@ def test_seek_mode_1():
def test_seek_mode_2():
# 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 @@ -61,68 +61,68 @@ def test_seek_mode_2():

def test_read_n0():
# 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
assert data == "7\nThis is line 8\n"
assert data == b"7\nThis is line 8\n"


def test_read_n():
# 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
assert data == "7\nT"
assert data == b"7\nT"


def test_read_eof():
# 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
assert data == ""
assert data == b""


def test_readline():
# 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
assert data == "This is line 1\n"
assert data == b"This is line 1\n"


def test_readlines():
# 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
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 5426bbc

Please sign in to comment.