Skip to content

Commit

Permalink
ssh: keep reading chunks if it is not possible to decode it
Browse files Browse the repository at this point in the history
It is possible that we don't read complete utf-8 data and then decode
fails. In this situation we have to keep reading data from ssh until
we read complete utf-8 data.
  • Loading branch information
pbrezina committed Jun 13, 2024
1 parent 57cbba4 commit 744b19a
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions pytest_mh/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,27 @@ def _read(self) -> str:
:rtype: str
"""
chunk: bytes = b""
while not self.eof:
self.channel.poll(timeout=1000, stderr=self.stderr)
chunk = self.channel.recv(stderr=self.stderr)
if chunk is None:
self.eof = True
return ""
new_chunk: bytes | None = self.channel.recv(stderr=self.stderr)

if not chunk:
if new_chunk is None:
self.eof = True
else:
chunk += new_chunk

try:
out = chunk.decode("utf-8")
return out
except UnicodeDecodeError:
# Error if we don't have anything more to read
if self.eof:
raise

# Otherwise concat with next chunk and see if we can decode it then
continue

return chunk.decode("utf-8")

return ""

def finish(self) -> None:
Expand Down

0 comments on commit 744b19a

Please sign in to comment.