Skip to content

Commit

Permalink
Possible fix for invalid utf-8 conversion.
Browse files Browse the repository at this point in the history
  • Loading branch information
rkorzeniewski committed Apr 24, 2018
1 parent 6304ac6 commit 069ea28
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
5 changes: 4 additions & 1 deletion libs/bconsole.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: UTF-8 -*-
from __future__ import unicode_literals
from subprocess import Popen, PIPE
from .utils import safe_unicode
import re
import utils

Expand All @@ -20,7 +21,9 @@ def bconsolecommand(cmd, api=False, timeout=True):
bcmd = cmd + '\n'
bconsole.stdin.write(bcmd.encode('utf-8'))
bconsole.stdin.close()
out = bconsole.stdout.read().decode('utf-8')
out = bconsole.stdout.read()
oututf8 = safe_unicode(out)
# .decode('utf-8')
bconsole.wait()
lines = out.splitlines()
return lines
Expand Down
12 changes: 11 additions & 1 deletion libs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,14 @@ def countbytes(fbyte, bs):
return int(float(fbyte) * 1048576 * 1048576)
if bs == 'P':
return int(float(fbyte) * 1024 * 1048576 * 1048576)
return int(float(fbyte))
return int(float(fbyte))


def safe_unicode(obj, *args):
""" return the unicode representation of obj """
try:
return unicode(obj, *args)
except UnicodeDecodeError:
# obj is byte string
ascii_text = str(obj).encode('string_escape')
return unicode(ascii_text)

0 comments on commit 069ea28

Please sign in to comment.