Correct str/bytes mixup in ContainerIO

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 commit is contained in:
Jon Dufresne 2019-11-03 12:51:40 -08:00 committed by Andrew Murray
parent d03d390a0d
commit e817ed0d3e
2 changed files with 23 additions and 23 deletions

View File

@ -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
@ -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
@ -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
@ -61,7 +61,7 @@ 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
@ -69,12 +69,12 @@ def test_read_n0():
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
@ -82,12 +82,12 @@ def test_read_n():
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
@ -95,34 +95,34 @@ def test_read_eof():
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

View File

@ -82,7 +82,7 @@ class ContainerIO:
else:
n = self.length - self.pos
if not n: # EOF
return ""
return b""
self.pos = self.pos + n
return self.fh.read(n)
@ -92,13 +92,13 @@ class ContainerIO:
: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