Skip to content
This repository has been archived by the owner on Jan 14, 2024. It is now read-only.

Commit

Permalink
#63: Fix unicode decode error
Browse files Browse the repository at this point in the history
  • Loading branch information
blackandred committed Dec 7, 2020
1 parent 08addeb commit 192cbd9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
3 changes: 3 additions & 0 deletions rkd/api/inputoutput.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,9 @@ def writelines(self, datas):
self.stream.writelines(datas)
self.stream.flush()

def fileno(self):
return self.stream.fileno()

def __getattr__(self, attr):
return getattr(self.stream, attr)

Expand Down
34 changes: 30 additions & 4 deletions rkd/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def write(self, text: str):

self.text += text

def trim_left_by(self, bytes: int):
self.text = self.text[bytes:]
def trim_left_by(self, chars: int):
self.text = self.text[chars:]

def get_value(self) -> str:
return self.text
Expand Down Expand Up @@ -160,14 +160,40 @@ def push_output(process, primary_fd, out_buffer: TextBuffer, process_state: Proc

# propagate to stdout
if o:
sys.stdout.write(o.decode('utf-8'))
decoded = carefully_decode(o, 'utf-8')

sys.stdout.write(decoded)
sys.stdout.flush()
out_buffer.write(o.decode('utf-8'))
out_buffer.write(decoded)

if process_state.has_exited:
return True


def carefully_decode(txt_as_bytes: bytes, enc: str) -> str:
"""
Decode from BYTES to STR.
In case of decode error attempt to decode a character-by-character to recover as much as possible
:param txt_as_bytes:
:param enc:
:return:
"""

try:
return txt_as_bytes.decode(enc)
except UnicodeDecodeError:
decoded = ''

for char in range(0, len(txt_as_bytes)):
try:
decoded += txt_as_bytes[char:char+1].decode(enc)
except UnicodeDecodeError:
pass

return decoded


def direct_debug_msg(text: str) -> None:
fd = open('/dev/stderr', 'w')
fd.write(text + "\n")
Expand Down

0 comments on commit 192cbd9

Please sign in to comment.