Merge pull request #4297 from radarhere/container

Change ContainerIO return type to match file object mode
This commit is contained in:
Hugo van Kemenade 2020-02-15 14:29:32 +02:00 committed by GitHub
commit cc3c743c70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 50 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,8 @@ def test_seek_mode_2():
def test_read_n0():
# Arrange
with open(TEST_FILE) as fh:
for bytesmode in (True, False):
with open(TEST_FILE, "rb" if bytesmode else "r") as fh:
container = ContainerIO.ContainerIO(fh, 22, 100)
# Act
@ -69,12 +70,15 @@ def test_read_n0():
data = container.read()
# Assert
if bytesmode:
data = data.decode()
assert data == "7\nThis is line 8\n"
def test_read_n():
# Arrange
with open(TEST_FILE) as fh:
for bytesmode in (True, False):
with open(TEST_FILE, "rb" if bytesmode else "r") as fh:
container = ContainerIO.ContainerIO(fh, 22, 100)
# Act
@ -82,12 +86,15 @@ def test_read_n():
data = container.read(3)
# Assert
if bytesmode:
data = data.decode()
assert data == "7\nT"
def test_read_eof():
# Arrange
with open(TEST_FILE) as fh:
for bytesmode in (True, False):
with open(TEST_FILE, "rb" if bytesmode else "r") as fh:
container = ContainerIO.ContainerIO(fh, 22, 100)
# Act
@ -95,23 +102,29 @@ def test_read_eof():
data = container.read()
# Assert
if bytesmode:
data = data.decode()
assert data == ""
def test_readline():
# Arrange
with open(TEST_FILE) as fh:
for bytesmode in (True, False):
with open(TEST_FILE, "rb" if bytesmode else "r") as fh:
container = ContainerIO.ContainerIO(fh, 0, 120)
# Act
data = container.readline()
# Assert
if bytesmode:
data = data.decode()
assert data == "This is line 1\n"
def test_readlines():
# Arrange
for bytesmode in (True, False):
expected = [
"This is line 1\n",
"This is line 2\n",
@ -122,12 +135,13 @@ def test_readlines():
"This is line 7\n",
"This is line 8\n",
]
with open(TEST_FILE) as fh:
with open(TEST_FILE, "rb" if bytesmode else "r") as fh:
container = ContainerIO.ContainerIO(fh, 0, 120)
# Act
data = container.readlines()
# Assert
if bytesmode:
data = [line.decode() for line in data]
assert data == expected

View File

@ -82,7 +82,7 @@ class ContainerIO:
else:
n = self.length - self.pos
if not n: # EOF
return ""
return b"" if "b" in self.fh.mode else ""
self.pos = self.pos + n
return self.fh.read(n)
@ -92,13 +92,14 @@ class ContainerIO:
:returns: An 8-bit string.
"""
s = ""
s = b"" if "b" in self.fh.mode else ""
newline_character = b"\n" if "b" in self.fh.mode else "\n"
while True:
c = self.read(1)
if not c:
break
s = s + c
if c == "\n":
if c == newline_character:
break
return s