From b06035fb1273ef2394b19ae7364504862d993455 Mon Sep 17 00:00:00 2001
From: Waleed El-Geresy <waleed.el-geresy@arm.com>
Date: Wed, 13 Jun 2018 12:08:47 +0100
Subject: [PATCH] Fix Python3 Byte and Regex Handling

Convert bytes to strings (utf-8 encoding) to make compatible with
Python3 in arm.py
Use the pattern property to extract the string from the regex pattern,
to pass as a string to tty.expect.
Drop problematic characters when decoding stdout and stderr in misc.py
by setting errors='replace' in the string decode method.
---
 devlib/platform/arm.py | 3 +++
 devlib/utils/misc.py   | 5 +++--
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/devlib/platform/arm.py b/devlib/platform/arm.py
index 6830ed8..43602e3 100644
--- a/devlib/platform/arm.py
+++ b/devlib/platform/arm.py
@@ -88,6 +88,9 @@ class VersatileExpressPlatform(Platform):
     def _init_android_target(self, target):
         if target.connection_settings.get('device') is None:
             addr = self._get_target_ip_address(target)
+            if sys.version_info[0] == 3:
+                # Convert bytes to string for Python3 compatibility
+                addr = addr.decode("utf-8")
             target.connection_settings['device'] = addr + ':5555'
 
     def _init_linux_target(self, target):
diff --git a/devlib/utils/misc.py b/devlib/utils/misc.py
index 0e8f173..34d9367 100644
--- a/devlib/utils/misc.py
+++ b/devlib/utils/misc.py
@@ -179,8 +179,9 @@ def check_output(command, timeout=None, ignore=None, inputtext=None, **kwargs):
     try:
         output, error = process.communicate(inputtext)
         if sys.version_info[0] == 3:
-            output = output.decode(sys.stdout.encoding)
-            error =  error.decode(sys.stderr.encoding)
+            # Currently errors=replace is needed as 0x8c throws an error
+            output = output.decode(sys.stdout.encoding, "replace")
+            error =  error.decode(sys.stderr.encoding, "replace")
     finally:
         if timeout:
             timer.cancel()