[U-Boot] [PATCH 10/21] patman: Provide a way to get program output in binary mode

Simon Glass sjg at chromium.org
Tue May 14 21:53:44 UTC 2019


At present cros_subprocess and the tools library use a string to obtain
stdout from a program. This works fine on Python 2. With Python 3 we end
up with unicode errors in some cases. Fix this by providing a binary mode,
which returns the data as bytes() instead of a string.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

 tools/patman/cros_subprocess.py | 15 +++++++++------
 tools/patman/tools.py           |  4 ++--
 2 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/tools/patman/cros_subprocess.py b/tools/patman/cros_subprocess.py
index 4de230ac63..39d4f21980 100644
--- a/tools/patman/cros_subprocess.py
+++ b/tools/patman/cros_subprocess.py
@@ -54,7 +54,7 @@ class Popen(subprocess.Popen):
     """
 
     def __init__(self, args, stdin=None, stdout=PIPE_PTY, stderr=PIPE_PTY,
-                 shell=False, cwd=None, env=None, **kwargs):
+                 shell=False, cwd=None, env=None, binary=False, **kwargs):
         """Cut-down constructor
 
         Args:
@@ -72,6 +72,7 @@ class Popen(subprocess.Popen):
         """
         stdout_pty = None
         stderr_pty = None
+        self.binary = binary
 
         if stdout == PIPE_PTY:
             stdout_pty = pty.openpty()
@@ -107,15 +108,17 @@ class Popen(subprocess.Popen):
             data: Data to convert, or None for ''
 
         Returns:
-            Converted data to a unicode string
+            Converted data, normally as a unicode string, but this uses bytes
+            in binary mode
         """
         if data is None:
             return ''
         else:
-            try:
-                data = data.decode('utf-8')
-            except UnicodeDecodeError:
-                data = data.decode('utf-8', 'ignore')
+            if not self.binary:
+                try:
+                    data = data.decode('utf-8')
+                except UnicodeDecodeError:
+                    data = data.decode('utf-8', 'ignore')
         return data
 
     def CommunicateFilter(self, output):
diff --git a/tools/patman/tools.py b/tools/patman/tools.py
index bf099798e6..1df8f2ecd2 100644
--- a/tools/patman/tools.py
+++ b/tools/patman/tools.py
@@ -167,9 +167,9 @@ def PathHasFile(fname):
             return True
     return False
 
-def Run(name, *args):
+def Run(name, *args, **kwargs):
     try:
-        return command.Run(name, *args, cwd=outdir, capture=True)
+        return command.Run(name, *args, cwd=outdir, capture=True, **kwargs)
     except:
         if not PathHasFile(name):
             msg = "Plesae install tool '%s'" % name
-- 
2.21.0.1020.gf2820cf01a-goog



More information about the U-Boot mailing list