Skip to content
This repository was archived by the owner on Dec 27, 2023. It is now read-only.

Commit ca2058a

Browse files
committed
Change IOBuffer API to accept strings only
Bytes to strings conversion was an API flaw that this patch will fix. In this way we threat every message coming from SUT as string and we won't get crazy with decode/encode operations.
1 parent 8f916d7 commit ca2058a

File tree

8 files changed

+32
-38
lines changed

8 files changed

+32
-38
lines changed

ltp/dispatcher.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,31 +174,29 @@ def __init__(self, test: Test) -> None:
174174
self._test = test
175175
self._line = ""
176176

177-
def write(self, data: bytes) -> None:
178-
data_str = data.decode(encoding="utf-8", errors="replace")
179-
180-
if len(data_str) == 1:
181-
self._line += data_str
182-
if data_str == "\n":
177+
def write(self, data: str) -> None:
178+
if len(data) == 1:
179+
self._line += data
180+
if data == "\n":
183181
ltp.events.fire(
184182
"test_stdout_line",
185183
self._test,
186184
self._line[:-1])
187185
self._line = ""
188186
else:
189-
lines = data_str.split('\n')
187+
lines = data.split('\n')
190188
for line in lines[:-1]:
191189
self._line += line
192190
ltp.events.fire("test_stdout_line", self._test, self._line)
193191
self._line = ""
194192

195193
self._line = lines[-1]
196194

197-
if data_str.endswith('\n') and self._line:
195+
if data.endswith('\n') and self._line:
198196
ltp.events.fire("test_stdout_line", self._test, self._line)
199197
self._line = ""
200198

201-
self.stdout += data_str
199+
self.stdout += data
202200

203201
def flush(self) -> None:
204202
pass
@@ -212,7 +210,7 @@ class RedirectStdout(IOBuffer):
212210
def __init__(self, sut: SUT) -> None:
213211
self._sut = sut
214212

215-
def write(self, data: bytes) -> None:
213+
def write(self, data: str) -> None:
216214
if not self._sut:
217215
return
218216

ltp/host.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,23 +115,22 @@ def force_stop(
115115
iobuffer: IOBuffer = None) -> None:
116116
self._inner_stop(signal.SIGKILL, timeout)
117117

118-
def _read_stdout(self, size: int, iobuffer: IOBuffer = None) -> bytes:
118+
def _read_stdout(self, size: int, iobuffer: IOBuffer = None) -> str:
119119
"""
120120
Read data from stdout.
121121
"""
122122
if not self.is_running:
123123
return None
124124

125125
data = os.read(self._proc.stdout.fileno(), size)
126+
rdata = data.decode(encoding="utf-8", errors="replace")
127+
rdata = rdata.replace('\r', '')
126128

127129
# write on stdout buffers
128130
if iobuffer:
129-
iobuffer.write(data)
131+
iobuffer.write(rdata)
130132
iobuffer.flush()
131133

132-
rdata = data.decode(encoding="utf-8", errors="replace")
133-
rdata = rdata.replace('\r', '')
134-
135134
return rdata
136135

137136
# pylint: disable=too-many-locals

ltp/qemu.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,23 +220,22 @@ def ping(self) -> float:
220220

221221
return exec_time
222222

223-
def _read_stdout(self, size: int, iobuffer: IOBuffer) -> bytes:
223+
def _read_stdout(self, size: int, iobuffer: IOBuffer) -> str:
224224
"""
225225
Read data from stdout.
226226
"""
227227
if not self.is_running:
228228
return None
229229

230230
data = os.read(self._proc.stdout.fileno(), size)
231+
rdata = data.decode(encoding="utf-8", errors="replace")
232+
rdata = rdata.replace('\r', '')
231233

232234
# write on stdout buffers
233235
if iobuffer:
234-
iobuffer.write(data)
236+
iobuffer.write(rdata)
235237
iobuffer.flush()
236238

237-
rdata = data.decode(encoding="utf-8", errors="replace")
238-
rdata = rdata.replace('\r', '')
239-
240239
return rdata
241240

242241
def _write_stdin(self, data: str) -> None:

ltp/session.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def __init__(self, sut: SUT, is_cmd: bool) -> None:
3636
self._sut = sut
3737
self._is_cmd = is_cmd
3838

39-
def write(self, data: bytes) -> None:
39+
def write(self, data: str) -> None:
4040
if self._is_cmd:
4141
ltp.events.fire("run_cmd_stdout", data)
4242
else:

ltp/sut.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class IOBuffer:
3232
IO stdout buffer. The API is similar to ``IO`` types.
3333
"""
3434

35-
def write(self, data: bytes) -> None:
35+
def write(self, data: str) -> None:
3636
"""
3737
Write data.
3838
"""

ltp/tests/sut.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
import pytest
77
import logging
88
import threading
9+
from ltp.sut import IOBuffer
910
from ltp.sut import SUTError
1011
from ltp.sut import SUTTimeoutError
1112
from ltp.utils import Timeout
1213

1314

14-
class Printer:
15+
class Printer(IOBuffer):
1516
"""
1617
stdout printer.
1718
"""
@@ -20,9 +21,8 @@ def __init__(self) -> None:
2021
self._logger = logging.getLogger("test.host")
2122
self._line = ""
2223

23-
def write(self, data):
24-
data_str = data.decode(encoding="utf-8", errors="replace")
25-
print(data_str, end="")
24+
def write(self, data: str) -> None:
25+
print(data, end="")
2626

2727
def flush(self):
2828
pass

ltp/tests/test_ssh.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ def test_reset_command(self, config):
7878
sut.communicate()
7979

8080
class MyBuffer(IOBuffer):
81-
data = bytes()
81+
data = ""
8282

83-
def write(self, data: bytes) -> None:
83+
def write(self, data: str) -> None:
8484
self.data = data
8585
# wait for data inside the buffer
8686
time.sleep(0.1)

ltp/ui.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,17 @@ def _user_friendly_duration(duration: float) -> str:
7979

8080
return uf_time
8181

82-
def _print_stdout(self, data: bytes) -> None:
82+
def _print_stdout(self, data: str) -> None:
8383
"""
8484
Print stdout coming from command run or test.
8585
"""
86-
data_str = data.decode(encoding="utf-8", errors="replace")
87-
88-
if len(data_str) == 1:
89-
self._line += data_str
90-
if data_str == "\n":
86+
if len(data) == 1:
87+
self._line += data
88+
if data == "\n":
9189
self._print(self._line, end='')
9290
self._line = ""
9391
else:
94-
lines = data_str.splitlines(True)
92+
lines = data.splitlines(True)
9593
if len(lines) > 0:
9694
for line in lines[:-1]:
9795
self._line += line
@@ -101,7 +99,7 @@ def _print_stdout(self, data: bytes) -> None:
10199

102100
self._line = lines[-1]
103101

104-
if data_str.endswith('\n') and self._line:
102+
if data.endswith('\n') and self._line:
105103
self._print(self._line, end='')
106104
self._line = ""
107105

@@ -133,7 +131,7 @@ def sut_restart(self, sut: str) -> None:
133131
def run_cmd_start(self, cmd: str) -> None:
134132
self._print(f"{cmd}", color=self.CYAN)
135133

136-
def run_cmd_stdout(self, data: bytes) -> None:
134+
def run_cmd_stdout(self, data: str) -> None:
137135
self._print_stdout(data)
138136

139137
def run_cmd_stop(self, command: str, stdout: str, returncode: int) -> None:
@@ -274,7 +272,7 @@ def __init__(self, no_colors: bool = False) -> None:
274272
ltp.events.register("test_completed", self.test_completed)
275273
ltp.events.register("test_stdout_line", self.test_stdout_line)
276274

277-
def sut_stdout_line(self, _: str, data: bytes) -> None:
275+
def sut_stdout_line(self, _: str, data: str) -> None:
278276
self._print_stdout(data)
279277

280278
def kernel_tained(self, message: str) -> None:

0 commit comments

Comments
 (0)