From 744b19a197720d207dc4d68489a28073e7ec564b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20B=C5=99ezina?= Date: Thu, 13 Jun 2024 13:53:18 +0200 Subject: [PATCH] ssh: keep reading chunks if it is not possible to decode it 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. --- pytest_mh/ssh.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/pytest_mh/ssh.py b/pytest_mh/ssh.py index bdafa95..9930960 100644 --- a/pytest_mh/ssh.py +++ b/pytest_mh/ssh.py @@ -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: