Skip to content

Commit

Permalink
Fixed UTF-8 decoding in Terminal widget (#2880)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlstevens committed Nov 4, 2021
1 parent 04a92a6 commit 8dfb3e9
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion panel/widgets/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,25 @@ def _reset(self):
def _remove_last_line_from_string(value):
return value[: value.rfind("CompletedProcess")]

def _decode_utf8_on_boundary(self, fd, max_read_bytes, max_extra_bytes=2):
"UTF-8 characters can be multi-byte so need to decode on correct boundary"
data = os.read(fd, max_read_bytes)
for _ in range(max_extra_bytes+1):
try:
return data.decode('utf-8')
except UnicodeDecodeError:
data = data + os.read(fd, 1)
raise UnicodeError('Could not find decode boundary for UTF-8')

def _forward_subprocess_output_to_terminal(self):
if not self._fd:
return
(data_ready, _, _) = select.select([self._fd], [], [], self._timeout_sec)
if not data_ready:
return
output = os.read(self._fd, self._max_read_bytes).decode()

output = self._decode_utf8_on_boundary(self._fd, self._max_read_bytes)

# If Child Process finished it will signal this by appending "CompletedProcess(...)"
if "CompletedProcess" in output:
self._reset()
Expand Down

0 comments on commit 8dfb3e9

Please sign in to comment.